QOJ.ac

QOJ

시간 제한: 1.0 s 메모리 제한: 256 MB 총점: 100 해킹 가능 ✓

#14109. 练习

통계

在一个班级里有 $2n$ 名学生。第 $i$ 名学生的水平值为 $c_i$。老师正在组织一次需要两人一组进行的练习。最初,对于每个从 $1$ 到 $n$ 的 $i$,学生 $2i - 1$ 和 $2i$ 被配对在一起。

现在,老师想要重新组成新的配对 $(a_1, b_1), (a_2, b_2), \dots, (a_n, b_n)$,并满足以下条件:

  • 每个学生恰好属于一个配对。也就是说,从 $1$ 到 $2n$ 的每个数字在 $a_1, a_2, \dots, a_n, b_1, b_2, \dots, b_n$ 中恰好出现一次。
  • 之前已经配对过的任意两名学生不能再次配对。也就是说,不存在 $1 \le i, j \le n$ 使得 $a_i = 2j - 1, b_i = 2j$ 或 $a_i = 2j, b_i = 2j - 1$。

老师希望最小化这 $n$ 个配对中水平值差值的绝对值之和,因为这样会更有效率。换句话说,$|c_{a_1} - c_{b_1}| + |c_{a_2} - c_{b_2}| + \dots + |c_{a_n} - c_{b_n}|$ 应该尽可能小。

老师能达到的 $|c_{a_1} - c_{b_1}| + |c_{a_2} - c_{b_2}| + \dots + |c_{a_n} - c_{b_n}|$ 的最小值是多少?

输入格式

输入的第一行包含一个整数 $n$ ($2 \le n \le 100\,000$)。

第二行包含 $2n$ 个整数 $c_1, c_2, \dots, c_{2n}$ ($0 \le c_i \le 10^9$) —— 代表学生的水平值。

输出格式

输出一个整数 —— 老师可以达到的 $|c_{a_1} - c_{b_1}| + |c_{a_2} - c_{b_2}| + \dots + |c_{a_n} - c_{b_n}|$ 的最小可能值。

样例

输入样例 1

2
1 2 3 4

输出样例 1

4

输入样例 2

3
1 9 3 4 2 6

输出样例 2

7

说明

在第一个样例中,老师可以将第一名学生与第三名学生配对,第二名学生与第四名学生配对,得到 $|3 - 1| + |4 - 2| = 4$。

在第二个样例中,老师可以将第一名学生与第三名学生配对,第二名学生与第六名学生配对,第四名学生与第五名学生配对,得到 $|1 - 3| + |9 - 6| + |4 - 2| = 7$。

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.