#P6117. [JOI 2019 Final] 硬币收藏 / Coin Collecting
[JOI 2019 Final] 硬币收藏 / Coin Collecting
Description
In Mr. JOI’s collection room, there is a huge table with many rare coins on it. To tidy up the table, he wants to rearrange the coins.
The tabletop can be viewed as a grid of size . Mr. JOI has coins. Initially, the -th coin is placed on the cell with coordinates . Mr. JOI’s goal is to place exactly one coin on every cell such that . To avoid damaging the coins, the only operation he can do is to choose one coin and move it to an adjacent cell (we say two cells are adjacent if and only if they share a common edge). During the moving process, it is allowed for two coins to be in the same cell. Mr. JOI wants to achieve the goal using as few operations as possible.
Given the number of coins and their initial positions, write a program to compute the minimum number of operations needed to achieve Mr. JOI’s goal.
Input Format
The first line contains an integer .
The next lines follow. The -th of these lines contains two integers and .
Output Format
Output one integer in one line, representing the minimum number of operations needed to achieve the goal.
3
0 0
0 4
4 0
2 1
2 5
-1 1
15
4
2 1
2 1
2 1
3 1
3 1
3 1
3 1
3 1
9
5
1000000000 1000000000
-1000000000 1000000000
-1000000000 -1000000000
1000000000 -1000000000
-1 -5
-2 2
2 8
4 7
-2 5
7 3
8000000029
Hint
Sample Explanation
Sample explanation 1:
One valid moving plan is:
Coin 1: $(0,0)\rightarrow(1,0)\rightarrow(1,1)\rightarrow(1,2)$
Coin 2: $(0,4)\rightarrow(1,4)\rightarrow(1,3)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,2)$
Coin 3:
Coin 4: does not move
Coin 5: $(2,5)\rightarrow(2,4)\rightarrow(2,3)\rightarrow(2,2)$
Coin 6:
It can be proven that Mr. JOI cannot achieve the goal with fewer than moves.
Constraints
Subtask 1 (8 points): .
Subtask 2 (29 points): .
Subtask 3 (63 points): no additional constraints.
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号