QOJ.ac

QOJ

시간 제한: 1.0 s 메모리 제한: 32 MB 총점: 80

#17131. 路费

통계

在一天中,Luka 的 $N$ 辆卡车在一条特定的高速公路上行驶。高速公路有许多出口和入口。具有特定编号的出口与具有该编号的入口位于同一位置。

进入高速公路时,卡车司机领到一张写有其所使用入口编号的通行卡。在驶离时,司机需要支付等同于入口编号与出口编号之差的绝对值的过路费。例如,如果通行卡上写着他使用了入口 $30$,那么在出口 $12$ 驶离将花费他 $18$。

Luka 想出了一种省钱的方法。任何两位司机都可以在高速公路上会面并交换通行卡,即使他们的路线没有重合。通行卡可以交换任意多次。

然而,如果司机的通行卡上写着他使用了与出口相同的入口,他就不能使用该出口,因为这会引起怀疑(即持有的卡上的入口编号不能等于驶离的出口编号)。

编写一个程序,计算司机们通过交换通行卡所能达到的最小过路费总额。

输入格式

第一行包含整数 $N$ ($1 \le N \le 100\,000$),表示卡车的数量。

接下来的 $N$ 行,每行包含两个介于 $1$ 和 $1\,000\,000$ 之间的整数。它们依次是一辆卡车的入口和出口编号。

没有两辆卡车会使用相同的高速公路入口,也没有两辆卡车会使用相同的出口。

输出格式

输出 Luka 的公司必须支付的最小过路费总额。

注意:请使用 64 位整数类型(C/C++ 中的 long long,Pascal 中的 int64)。

样例

输入 1

3
3 65
45 10
60 25

输出 1

32

输入 2

3
5 5
6 7
8 8

输出 2

5

说明

在第一个样例中,第一位和第三位司机将交换通行卡。在此之后,第二位和第三位司机交换通行卡。在这之后,司机们将分别持有入口为 $60$、$3$、$45$ 的通行卡。总过路费为 $|65-60| + |10-3| + |25-45| = 32$。

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.