Backup Towers
시간 제한3초메모리 제한2048 MB
격자 위 모든 칸에서 맨해튼 거리로 가장 가까운 타워와 두 번째로 가까운 타워의 번호를 구하고, 거리가 같으면 번호가 작은 쪽을 고른다.
문제
The nation of Gridlandia can be, naturally, represented as a grid of houses.
Gridlandia is planning to install a number cell towers at some of the points of the grid. When a resident connects to cellular data, their phone will try to connect first to their closest tower, and if that fails, to their second closest tower. The government of Gridlandia is worried that some towers will become much more overloaded than others. Help them by computing for each house both the closest and second closest cell tower, as measured by the Manhattan Distance, which is the difference in rows plus the difference in columns.
입력
The first line of input contains three integers , () and () where Gridlandia is a grid with rows and columns, and is the number of cell towers. The towers are numbered from to .
Each of the next lines contains two integers and (, ) which are the positions of the cell towers, in order. Every pair will be unique.
출력
The first lines of output should each contain space separated integers. The integer on the row and column should be the number of the closest cell tower to position , as measured by the Manhattan distance.
The next lines of output should also each contain space separated integers. The integer on the row and column should be the number of the second closest cell tower to position , as measured by the Manhattan distance.
If there are multiple closest cell towers to a given position, output the one with the lowest number. Likewise, if there are multiple second-closest cell towers to a given position, output the one with the lowest number.