Universal Cup Judging System

Universal Cup

Limite de temps : 2.0 s Limite de mémoire : 1024 MB Points totaux : 100 Hackable ✓
Statistiques

有一栋楼有 $10^9$ 层,但只有 1 部电梯。初始时,电梯位于 $f$ 层。

有 $n$ 个人在等待电梯。第 $i$ 个人目前在 $l_i$ 层,想要乘坐电梯前往 $r_i$ 层($l_i < r_i$)。由于电梯很小,它一次最多只能搭载 1 个人。

电梯向上移动 1 层需要消耗 1 单位电能。如果电梯向下移动,则不需要消耗电能。也就是说,将电梯从 $x$ 层移动到 $y$ 层需要消耗 $\max(y - x, 0)$ 单位电能。

请找到一种最优的接送顺序,使得总电能消耗最小。

更正式地说,设 $a_1, a_2, \dots, a_n$ 是 $n$ 的一个排列,其中 $a_i$ 表示第 $i$ 个乘坐电梯的人是 $a_i$。总电能消耗可以计算为:

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

其中为了方便起见,令 $a_0 = 0, r_0 = f$。

回想一下,长度为 $n$ 的序列 $a_1, a_2, \dots, a_n$ 是 $n$ 的一个排列,当且仅当从 $1$ 到 $n$ 的每个整数(包含 $1$ 和 $n$)在序列中恰好出现一次。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$ ($1 \le T \le 10^4$),表示测试数据的组数。对于每组测试数据:

第一行包含两个整数 $n$ 和 $f$ ($1 \le n \le 10^5, 1 \le f \le 10^9$),分别表示人数和电梯的初始位置。

接下来的 $n$ 行中,第 $i$ 行包含两个整数 $l_i$ 和 $r_i$ ($1 \le l_i < r_i \le 10^9$),表示第 $i$ 个人想要从 $l_i$ 层乘坐电梯前往 $r_i$ 层。

保证所有测试数据的 $n$ 之和不超过 $3 \times 10^5$。

输出格式

对于每组测试数据,首先输出一行,包含一个整数,表示最小总电能消耗;然后输出另一行,包含 $n$ 个整数 $a_1, a_2, \dots, a_n$,中间用空格分隔,表示运送所有人的最优顺序。注意,这 $n$ 个整数必须构成 $n$ 的一个排列。如果存在多个最优顺序,输出其中任意一个即可。

样例

样例输入 1

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

样例输出 1

11
2 1 4 3
5
2 1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.