Input description
The first line of my input contains two numbers: 𝑛 and 𝑑, denoting respectively the number of containers (2 ≤ 𝑛 ≤ 20,000) and the number of deliveries (not more than 100,000)
In the following 𝑑 lines there are pairs of numbers: 𝑤 and 𝑧, where 𝑤 is the number of wagons in the delivery
(no more than 20,000) and 𝑧 is the number of gravel in each wagon (no more than 20,000).
The total number of wagons from all deliveries will not exceed 1,000,000.
There will be no situation in any of the inputs where the container will contain more
than 1,000,000,000 gravel.
Problem statement
Wagons are emptied one at a time, evenly into two consecutive containers. These are the rules for determining which two adjacent containers are chosen (where containers[] denotes a list of the n current container sizes):
- Among all pairs
(containers[i], containers[i+1])with0 <= i < n - 1, choose the pair which minimizesmin(containers[i], containers[i+1]) - If there is a tie, choose among the tied pairs, the pair which minimizes
max(containers[i], containers[i+1])(i.e. minimizes the second smallest element in the pair) - If there is still a tie, choose the pair
(containers[i], containers[i+1])with minimum indexiamong the tied pairs.
The gravel is distributed equally into the pair. If there is an odd number of gravel in a wagon, the container with the smaller ID gains the surplus (1 pebble).
At first, all containers are empty.
For example:
lista = [0, 4, 3, 2, 0, 5, 4, 0, 3, 4, 2, 0]
My first smallest value in that list is 0 so there are 6 possibilities for different pairs
0,4 with indices 0 and 1
0,2 with indices 3 and 4
0,5 with indices 4 and 5
4,0 with indices 6 and 7
2,0 with indices 10 and 11
My second smallest value in this 6 possibilities is 2 so I have two options
0,2 with indices 3 and 4
2,0 with indices 10 and 11
Indexes 3 and 4 are less than 10 and 11 so we will be dropping the wagons into containers with indexes [3, 4]
In the output:
Each of the 𝑛 lines of output data should contain a single number - the number of pebbles
in a container. The order of listing should be the same as the order of the identifiers.
Example
For the example input shown below:
6 4
4 3
1 2
2 14
4 3
the correct answer is:
6
10
12
7
11
8
Explanation of the example
In the first delivery:
- The first wagon can be unloaded into pairs of containers
(1, 2), (2, 3), (3, 4), (4, 5), (5, 6).
We choose the first one and the values of the filling are(2 1 0 0 0 0). - Second wagon can be put into pairs of containers
(3, 4), (4, 5), (5, 6).
We choose the first one and the filling values are(2 1 2 1 0 0). - The third wagon can be put into a pair of containers
(5, 6), which will give us the values of the fills
(2 1 2 1 2 1). - The fourth wagon may be discharged into par
containers
(1, 2), (2, 3), (3, 4), (4, 5), (5, 6). We choose the first one and the filling values is(4 2 2 1 2 1).
- The first wagon can be unloaded into pairs of containers
The second delivery changes the capacity of the containers as follows:
(4 2 3 2 2 1).In the third delivery:
- The first wagon may be unloaded only into a pair of containers
(5, 6), and then the values for the fills are (4 2 3 2 9 8). - The second car can be unloaded into pairs
(2, 3)or(3, 4). We choose the first one and the fill values are(4 9 10 2 9 8).
- The first wagon may be unloaded only into a pair of containers
The last delivery changes the fills as follows:
(4 9 10 4 10 8),(6 10 10 4 10 8),(6 10 12 5 10 8),(6 10 12 7 11 8)
Below is my program code (it works, however, it is not optimal for large numbers)
n, d = map(int, input().split())
containers = []
for _ in range(n):
containers.append(0)
for _ in range(d):
wagons, gravel = map(int, input().split()) # w, z
half_gravel = int(gravel/2)
greater_half = int(gravel/2)+ gravel%2
for _ in range(wagons):
i = min(range(len(containers) - 1),
key=lambda i: sorted(containers[i : i+2]))
containers[i] += greater_half
containers[i+1] += half_gravel
for i in containers:
print(i)
Thanks in advance for your help in optimizing the program
containers.append(0)in a loop instead ofcontainers = [0]*n, but the bigger problem is algorithmic. Have you learned/ researched any 'data structures for finding the minimum', such as priority queues or binary search trees?