QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 256 MB Puntuación total: 100

#2765. 硬币收集

Estadísticas

JOI 先生的收藏室里有一张巨大的桌子,上面放着一些稀有的硬币。为了清理桌子,他打算重新排列这些硬币。

这张桌子可以看作是一个 $2\,000\,000\,001 \times 2\,000\,000\,001$ 的网格。列的编号从左到右依次为 $-1\,000\,000\,000$ 到 $1\,000\,000\,000$,行的编号从下到上依次为 $-1\,000\,000\,000$ 到 $1\,000\,000\,000$。列号为 $x$、行号为 $y$ 的格子记作 $(x, y)$。

桌上共有 $2N$ 枚硬币。目前,第 $i$ 枚硬币($1 \le i \le 2N$)放置在格子 $(X_i, Y_i)$ 上。JOI 先生的目标是将硬币放置在所有满足 $1 \le x \le N$ 且 $1 \le y \le 2$ 的格子 $(x, y)$ 上。为了不损坏硬币,他唯一能进行的操作是选择一枚硬币,并将其移动到相邻的格子(两个格子相邻当且仅当它们共用一条边)。在移动过程中,允许在同一个格子上放置多枚硬币。他希望以尽可能少的操作次数达到目标。

请编写一个程序,在给定硬币数量和硬币当前位置的情况下,计算达到目标所需的最少操作次数。

输入格式

从标准输入读取以下数据:

$N$ $X_1 \ Y_1$ $\vdots$ $X_{2N} \ Y_{2N}$

输出格式

将结果输出到标准输出。输出应包含达到目标所需的最少操作次数。

数据范围

  • $1 \le N \le 100\,000$
  • $-1\,000\,000\,000 \le X_i \le 1\,000\,000\,000$ ($1 \le i \le 2N$)
  • $-1\,000\,000\,000 \le Y_i \le 1\,000\,000\,000$ ($1 \le i \le 2N$)

子任务

  1. (8 分) $N \le 10$
  2. (29 分) $N \le 1\,000$
  3. (63 分) 无附加限制

样例

样例输入 1

3
0 0
0 4
4 0
2 1
2 5
-1 1

样例输出 1

15

说明

在此样例中,6 枚硬币的初始位置如图所示。目标是将硬币收集到粗线框内的区域。

例如,JOI 先生可以通过以下移动以 15 次操作达到目标: 第 1 枚硬币:$(0, 0) \to (1, 0) \to (1, 1) \to (1, 2)$ 第 2 枚硬币:$(0, 4) \to (1, 4) \to (1, 3) \to (2, 3) \to (3, 3) \to (3, 2)$ 第 3 枚硬币:$(4, 0) \to (4, 1) \to (3, 1)$ 第 5 枚硬币:$(2, 5) \to (2, 4) \to (2, 3) \to (2, 2)$ * 第 6 枚硬币:$(-1, 1) \to (0, 1) \to (1, 1)$

由于无法用 14 次或更少的操作达到目标,因此应输出 15。

样例输入 2

4
2 1
2 1
2 1
3 1
3 1
3 1
3 1
3 1

样例输出 2

9

说明

同一个格子上可能放置多枚硬币。

样例输入 3

5
1000000000 1000000000
-1000000000 1000000000
-1000000000 -1000000000
1000000000 -1000000000
-1 -5
-2 2
2 8
4 7
-2 5
7 3

样例输出 3

8000000029

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.