#P14191. [ICPC 2024 Hangzhou R] Elevator II

    ID: 14064 Type: RemoteJudge 2000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>贪心2024Special Judge构造ICPC杭州

[ICPC 2024 Hangzhou R] Elevator II

题目描述

There is a building with 10910^9 floors but only 11 elevator. Initially, the elevator is on the ff-th floor.

There are nn people waiting for the elevator. The ii-th person is currently on the lil_i-th floor and wants to take the elevator to the rir_i-th floor (li<ril_i < r_i). Because the elevator is so small, it can carry at most 11 person at a time.

It costs 11 unit of electric energy to move the elevator 11 floor upwards. No energy is needed if the elevator moves downwards. That is to say, it costs max(yx,0)\max(y - x, 0) units of electric energy to move the elevator from the xx-th floor to the yy-th floor.

Find the optimal order to take all people to their destinations so that the total electric energy cost is minimized.

More formally, let a1,a2,,ana_1, a_2, \cdots, a_n be a permutation of nn where aia_i indicates that the ii-th person to take the elevator is aia_i. The total electric energy cost can be calculated as

$$\sum\limits_{i = 1}^n (\max(l_{a_i} - r_{a_{(i - 1)}}, 0) + r_{a_i} - l_{a_i}) $$

where a0=0,r0=fa_0 = 0, r_0 = f for convenience.

Recall that a sequence a1,a2,,ana_1, a_2, \cdots, a_n of length nn is a permutation of nn if and only if each integer from 11 to nn (both inclusive) appears exactly once in the sequence.

输入格式

There are multiple test cases. The first line of the input contains an integer TT (1T1041 \le T \le 10^4) indicating the number of test cases. For each test case:

The first line contains two integers nn and ff (1n1051 \le n \le 10^5, 1f1091 \le f \le 10^9) indicating the number of people and the initial position of the elevator.

For the following nn lines, the ii-th line contains two integers lil_i and rir_i (1li<ri1091 \le l_i < r_i \le 10^9) indicating that the ii-th person wants to go from the lil_i-th floor to the rir_i-th floor by elevator.

It's guaranteed that the sum of nn of all test cases will not exceed 3×1053 \times 10^5.

输出格式

For each test case, first output one line containing one integer indicating the minimum total electric energy, then output another line containing nn integers a1,a2,,ana_1, a_2, \cdots, a_n separated by a space indicating the optimal order to carry all people. Note that these nn integers must form a permutation of nn. If there are multiple optimal orders, you can print any of them.

2
4 2
3 6
1 3
2 7
5 6
2 5
2 4
6 8
11
2 1 4 3
5
2 1