시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB58401955.882%

문제

A heated game of “chess-tidying” starts with a messy board containing several king pieces strewn across the cells. The player’s job is to put all of the pieces into a line along the primary diagonal of the board, which runs along black squares.

In each move, unlike in normal chess, one king may be moved one place either horizontally or vertically (but not both) to another unoccupied cell.

Given an instance of the game, find how many moves you will need in order to finish it.

Figure K.1: Illustration of Sample Input 2. In this case, the minimum number of moves necessary to put all of the kings along the black diagonal is 28 as pictured.

입력

  • One line with the number of rows and columns, n (1 ≤ n ≤ 500).
  • Each of the following n lines contains the two-dimensional integer coordinates c and r (1 ≤ c, r ≤ n), the position of one of the kings.

Each of the kings starts at a unique position.

출력

Output the minimum number of moves necessary to cover the main diagonal (r = c) with kings.

예제 입력 1

3
1 1
2 3
3 2

예제 출력 1

2

예제 입력 2

8
6 4
6 8
5 5
5 4
4 8
5 7
7 4
3 7

예제 출력 2

28

출처

ICPC > Regionals > Europe > Northwestern European Regional Contest > The UK & Ireland Programming Contest > UKIEPC 2018 K번

  • 문제를 만든 사람: Robin Lee