#P11049. [IOI 2024] 尼罗河船运

[IOI 2024] 尼罗河船运

Background

This is an interactive problem. You only need to implement the function required in the code.

Your code must not include any additional header files, and you do not need to implement the main function.

This problem only supports submissions in C++. However, please do not use C++14 (GCC 9).

Problem Description

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

To transport these handicrafts, you use special boats. Each boat can carry at most two handicrafts.

  • If you decide to put a single handicraft on a boat, its weight can be arbitrary.
  • If you want to put two handicrafts on the same boat, you must ensure the boat is balanced. Specifically, if handicrafts pp and qq (0p<q<N0 \leq p < q < N) have an absolute weight difference of at most DD, i.e., W[p]W[q]D|W[p] - W[q]| \leq D, then you may place them on the same boat.

You must pay to transport a handicraft, and the cost depends on how many handicrafts are carried on the same boat. The transport cost for handicraft ii (0i<N0 \leq i < N) is:

  • A[i]A[i], if you transport handicraft ii alone on a boat, or
  • B[i]B[i], if you transport handicraft ii together with another handicraft.

Note that in the second case, you pay the cost for both handicrafts on the boat. Specifically, if you decide to transport handicrafts pp and qq (0p<q<N0 \leq p < q < N) on the same boat, you need to pay B[p]+B[q]B[p] + B[q].

The cost of transporting a handicraft alone on a boat is always higher than transporting it together with another handicraft, so for every ii satisfying 0i<N0 \leq i < N, we have B[i]<A[i]B[i] < A[i].

Unfortunately, because the Nile is unpredictable, the value of DD changes frequently. Your task is to answer QQ queries, numbered from 00 to Q1Q-1. The queries are described by an array EE of length QQ. The answer to query jj (0j<Q0 \leq j < Q) is the minimum total cost to transport all NN handicrafts when DD is equal to E[j]E[j].

Input Format

The grader reads the input data in the following order:

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

The grader outputs the following in order:

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

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

5
15 5 1
12 4 2
2 5 2
10 6 3
21 3 2
3
5
9
1

16
11
23

Hint

Implementation Details

You need to implement the following function.

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: integer arrays of length NN, giving the weights and costs of the handicrafts.
  • EE: an integer array of length QQ, giving the value of DD in each query.
  • The function should return an array RR containing QQ integers, giving the minimum total transport cost, where R[j]R[j] corresponds to the cost when DD equals E[j]E[j] (for each jj satisfying 0j<Q0 \leq j < Q).
  • For each test case, this function is called exactly once.

Constraints

  • 1N1000001 \leq N \leq 100\,000.
  • 1Q1000001 \leq Q \leq 100\,000.
  • For each ii satisfying 0i<N0 \leq i < N, 1W[i]1091 \leq W[i] \leq 10^{9}.
  • For each ii satisfying 0i<N0 \leq i < N, 1B[i]<A[i]1091 \leq B[i] < A[i] \leq 10^{9}.
  • For each jj satisfying 0j<Q0 \leq j < Q, 1E[j]1091 \leq E[j] \leq 10^{9}.

Subtasks

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

Hint

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 handicrafts and Q=3Q=3 queries.

In the first query, D=5D = 5. You can put handicrafts 00 and 33 on the same boat (because 15105|15 - 10| \leq 5), and put all other handicrafts on separate boats. This makes the minimum total cost to transport all handicrafts equal to 1+4+5+3+3=161+4+5+3+3 = 16.

In the second query, D=9D = 9. You can put handicrafts 00 and 11 on the same boat (because 15129|15 - 12| \leq 9), and put handicrafts 22 and 33 on the same boat (because 2109|2 - 10| \leq 9). The remaining handicraft is transported alone on a boat. This makes the minimum total cost to transport all handicrafts equal to 1+2+2+3+3=111+2+2+3+3 = 11.

In the last query, D=1D = 1. You need to transport each handicraft alone on a boat. This makes the minimum total cost to transport all handicrafts equal to 5+4+5+6+3=235+4+5+6+3 = 23.

Therefore, the function should return [16,11,23][16, 11, 23].

Translated by ChatGPT 5