Codeforces Round 126 (Div. 2)
A
Cinema
Codeforces
2400
原题
←B →
翻译中…

The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into $*n*$ rows, each row consists of $*m*$ seats.

翻译中…

There are $*k*$ people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates $(*x*_{*i*}, *y*_{*i*})$, where $*x*_{*i*}$ is the row number, and $*y*_{*i*}$ is the seat number in this row.

翻译中…

It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat $(*x*_{1}, *y*_{1})$, then when he comes to the box office, he chooses such empty seat $(*x*_{2}, *y*_{2})$, which satisfies the following conditions:

翻译中…
  • the value of $|*x*_{1} - *x*_{2}| + |*y*_{1} - *y*_{2}|$ is minimum
  • if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of $*x*_{2}$ is minimum
  • if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of $*y*_{2}$ is minimum
翻译中…

Your task is to find the coordinates of a seat for each person.

翻译中…

Input

翻译中…

The first input line contains three integers $*n*$, $*m*$, $*k*$ ($1 ≤ *n*, *m* ≤ 2000$, $1 ≤ *k* ≤ *min*(*n*·*m*, 10^{5}$) — the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next $*k*$ lines contains two integers $*x*_{*i*}$, $*y*_{*i*}$ ($1 ≤ *x*_{*i*} ≤ *n*$, $1 ≤ *y*_{*i*} ≤ *m*$) — the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line).

翻译中…

Output

翻译中…

Print $*k*$ lines, each containing a pair of integers. Print on the $*i*$-th line $*x*_{*i*}, *y*_{*i*}$ — the coordinates of the seat, for which the person who stands $*i*$-th in the line will buy the ticket.

翻译中…

样例

Input #1
3 4 6
1 1
1 1
1 1
1 2
1 3
1 3
Output #1
1 1
1 2
2 1
1 3
1 4
2 3
InputCopy OutputCopy
Input #2
4 3 12
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
2 2
Output #2
2 2
1 2
2 1
2 3
3 2
1 1
1 3
3 1
3 3
4 2
4 1
4 3
InputCopy OutputCopy
题目信息
题号
200A
时间限制
1.5 s
内存限制
256 MB
标签
暴力
数据结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。