IOI '18 P6 - Meetings

View as PDF

Submit solution

Points: 50 (partial)
Time limit: 4.5s
Memory limit: 768M

Problem type
Allowed languages
C++

There are N mountains lying in a horizontal row, numbered from through from left to right. The height of the mountain i is H_i (0 \leq i \leq N -1). Exactly one person lives on the top of each mountain.

You are going to hold Q meetings, numbered from 0 through Q-1. The meeting j (0 \leq j \leq Q-1) will be attended by all the people living on the mountains from L_j to R_j, inclusive (0 \leq L_j \leq R_j \leq N-1). For this meeting, you must select a mountain x as the meeting place (l_j \leq x \leq R_j). The cost of this meeting, based on your selection, is then calculated as follows:

  • The cost of the participant from each mountain y (L_j \leq y \leq R_j) is the maximum height of the mountains between the mountains x and y, inclusive. In particular, the cost of the participant from the mountain x is H_x, the height of the mountain x.
  • The cost of the meeting is the sum of the costs of all participants.

For each meeting, you want to find the minimum possible cost of holding it.

Note that all participants go back to their own mountains after each meeting; so the cost of a meeting is not influenced by the previous meetings.

Implementation Details

You should implement the following function:

std::vector<long long> mininum_costs(std::vector<int> H, std::vector<int> L, std::vector<int> R);
  • H: an array of length N, representing the heights of the mountains.
  • L and R: arrays of length Q, representing the range of the participants in the meetings.
  • This function should return an array C of length Q. The value of C_j (1 \leq j \leq Q-1) must be the minimum possible cost of holding the meeting j.
  • Note that the values of N and Q are the lengths of the arrays, and can be obtained as indicated in the implementation notice.

Example

Let N = 4, H = [2, 4, 3, 5], Q  = 2, L = [0, 1], and R = [2,3].

The grader calls minimum_costs([2, 4, 3, 5], [0, 1], [2, 3]).

The meeting j = 0 has L_j = 0 and R_j = 2, so will be attended by the people living on the mountains 0, 1, and 2. If the mountain 0 is chosen as the meeting place, the cost of meeting 0 is calculated as follows:

  • The cost of the participant from the mountain 0 is \max\{H_0\} = 2.
  • The cost of the participant from the mountain 1 is \max\{H_0, H_1\} = 4.
  • The cost of the participant from the mountain 2 is \max\{H_0,H_1,H_2\} = 4.
  • Therefore the cost of the meeting 0 is 2 + 4 + 4 = 10.

It is impossible to hold the meeting 0 at a lower cost, so the minimum cost of the meeting 0 is 10.

The meeting j = 1 has L_j = 1 and R_j = 3, so will be attended by the people living on the mountains 1, 1,, and 3. If the mountain 2 is chosen as the meeting place the cost of the meeting 1 is calculated as follows:

  • The cost of the participant from the mountain 1 is \max\{H_1, H_2\} = 4.
  • The cost of the participant from the mountain 2 is \max\{H_2\} = 3.
  • The cost of the participant from the mountain 3 is \max\{H_2, h_3\} = 5.
  • Therefore the cost of the meeting 1 is 4 + 3 + 5 = 12.

It is impossible to hold the meeting 1 at a lower cost, so the minimum cost of the meeting 1 is 12.

Constraints

  • 1 \leq N \leq 750\,000
  • 1 \leq Q \leq 750\,000
  • 1 \leq H_i \leq 1\,000\,000\,000 (0 \leq i \leq N - 1)
  • 0 \leq L_j \leq R_j \leq N - 1 (0 \leq  \leq Q - 1)
  • (L_j, R_j) \neq (L_k,R_k) (0 \leq j < k \leq Q-1)

Subtasks

  1. (4 points) N \leq 3\,000, Q \leq 10
  2. (15 points) N \leq 5\,000, Q \leq 5\,000
  3. (17 points) N \leq 100\,000, Q \leq 100\,000, H_i \leq 2 (0 \leq i \leq N-1)
  4. (24 points) N \leq 100\,000, Q \leq 100\,000, H_i \leq 20 (0 \leq i \leq N-1)
  5. (40 points) No additional constraints

Comments

There are no comments at the moment.