View problem - Nile (IOI24_nile)

Time limitMemory limit# of submissions# of submitted usersSolved #Accepted user ratio
2000 ms2048 MiB6021147767.54%

You want to transport NN artifacts through the Nile. The artifacts are numbered from 00 to N1N-1. The weight of artifact ii (0i<N0 \leq i < N) is W[i]W[i].

To transport the artifacts, you use specialized boats. Each boat can carry at most two artifacts.

  • If you decide to put a single artifact in a boat, the artifact weight can be arbitrary.
  • If you want to put two artifacts in the same boat, you have to make sure the boat is balanced evenly. Specifically, you can send artifacts pp and qq (0p<q<N0 \leq p < q < N) in the same boat only if the absolute difference between their weights is at most DD, that is W[p]W[q]D|W[p] - W[q]| \leq D.

To transport an artifact, you have to pay a cost that depends on the number of artifacts carried in the same boat. The cost of transporting artifact ii (0i<N0 \leq i < N) is:

  • A[i]A[i], if you put the artifact in its own boat, or
  • B[i]B[i], if you put it in a boat together with some other artifact.

Note that in the latter case, you have to pay for both artifacts in the boat. Specifically, if you decide to send artifacts pp and qq (0p<q<N0 \leq p < q < N) in the same boat, you need to pay B[p]+B[q]B[p] + B[q].

Sending an artifact in a boat by itself is always more expensive than sending it with some other artifact sharing the boat with it, so B[i]<A[i]B[i] < A[i] for all ii such that 0i<N0 \leq i < N.

Unfortunately, the river is very unpredictable and the value of DD changes often. Your task is to answer QQ questions numbered from 00 to Q1Q-1. The questions are described by an array EE of length QQ. The answer to question jj (0j<Q0 \leq j < Q) is the minimum total cost of transporting all NN artifacts, when the value of DD is equal to E[j]E[j].

Implementation Details

You should implement the following procedure.

std::vector<long long> calculate_costs(
    std::vector<int> W, std::vector<int> A, 
    std::vector<int> B, std::vector<int> E)
  • WW, AA, BB: arrays of integers of length NN, describing the weights of the artifacts and the costs of transporting them.
  • EE: an array of integers of length QQ describing the value of DD for each question.
  • This procedure should return an array RR of QQ integers containing the minimum total cost of transporting the artifacts, where R[j]R[j] gives the cost when the value of DD is E[j]E[j] (for each jj such that 0j<Q0 \leq j < Q).
  • This procedure is called exactly once for each test case.

Constraints

  • 1N100,0001 \leq N \leq 100,000
  • 1Q100,0001 \leq Q \leq 100,000
  • 1W[i]1091 \leq W[i] \leq 10^{9} for each ii such that 0i<N0 \leq i < N
  • 1B[i]<A[i]1091 \leq B[i] < A[i] \leq 10^{9} for each ii such that 0i<N0 \leq i < N
  • 1E[j]1091 \leq E[j] \leq 10^{9} for each jj such that 0j<Q0 \leq j < Q

Subtasks

Subtask Score Additional Constraints
1 66 Q5Q \leq 5; N2000N \leq 2000; W[i]=1W[i] = 1 for each ii such that 0i<N0 \leq i < N
2 1313 Q5Q \leq 5; W[i]=i+1W[i] = i+1 for each ii such that 0i<N0 \leq i < N
3 1717 Q5Q \leq 5; A[i]=2A[i] = 2 and B[i]=1B[i] = 1 for each ii such that 0i<N0 \leq i < N
4 1111 Q5Q \leq 5; N2000N \leq 2000
5 2020 Q5Q \leq 5
6 1515 A[i]=2A[i] = 2 and B[i]=1B[i] = 1 for each ii such that 0i<N0 \leq i < N
7 1818 No additional constraints.

Example

Consider the following call.

calculate_costs([15, 12, 2, 10, 21],
                [5, 4, 5, 6, 3],
                [1, 2, 2, 3, 2],
                [5, 9, 1])

In this example we have N=5N = 5 artifacts and Q=3Q = 3 questions.

In the first question, D=5D = 5. You can send artifacts 00 and 33 in one boat (since 15105|15 - 10| \leq 5) and the remaining artifacts in separate boats. This yields the minimum cost of transporting all the artifacts, which is 1+4+5+3+3=161+4+5+3+3 = 16.

In the second question, D=9D = 9. You can send artifacts 00 and 11 in one boat (since 15129|15 - 12| \leq 9) and send artifacts 22 and 33 in one boat (since 2109|2 - 10| \leq 9). The remaining artifact can be sent in a separate boat. This yields the minimum cost of transporting all the artifacts, which is 1+2+2+3+3=111+2+2+3+3 = 11.

In the final question, D=1D = 1. You need to send each artifact in its own boat. This yields the minimum cost of transporting all the artifacts, which is 5+4+5+6+3=235+4+5+6+3 = 23.

Hence, this procedure should return [16,11,23][16, 11, 23].

Sample Grader

Input format:

N
W[0] A[0] B[0]
W[1] A[1] B[1]
...
W[N-1] A[N-1] B[N-1]
Q
E[0]
E[1]
...
E[Q-1]

Output format:

R[0]
R[1]
...
R[S-1]

Here, SS is the length of the array RR returned by calculate_costs.

Attachments
File nameSize
nile.zip2.41 KiB