codeforces-200aCinema
| Time Limit: 1500MS |
|
Memory Limit: 262144KB |
|
64bit IO Format: %i64d &%i64u |
Submit Status Description The capital of Berland have the only movie theater in the country. Besides, it consists of only one. The divided Into n rows, each row Consists Of m seats. There are k people lined up to the box Office, wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat, which 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's possible that some people has chosen the same place and 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 have the following Logic:let ' s assume that he originally wanted to buy a ticket to Seat x 1,? Y 1) , then if he comes to the box office, he chooses such empty Seat&nbs P ( x 2,? Y 2) , which satisfies the following conditions:
- The value of | x1?-? x2|? +?| y 1?-? y 2| is minimum
- If the choice is isn't unique, then among the seats that satisfy the first condition, this person selects the one for which The value of x2 is minimum
- If the choice is still isn't unique, among the seats that satisfy the first and second conditions, this person selects the O NE for which the value of y2 is minimum
Your task is to find the coordinates of a seat for each person. Input The first input line contains three integersN,m,k(1?≤? n,? m? ≤?2000,1?≤? k? ≤? min (n· m,?5 -the number of rows in the guest, the number of seats in each row and the number of people in the line, correspondingly. Each of the nextklines contains, integers xi , yi (1?≤? x i? ≤? N ,1?≤? y i? ≤? m -the coordinates of the seat each have chosen. Numbers on the same line is separated by a space. The pairs of coordinates is located in the order, in which people stand on the line, and starting from the head (the first PE Rson in the "stands in front of the box office" to the tail (the last person on the line). Output Print k lines, each containing a pair of integers. Print on the i-th line xi,? Yi -the coordinates of the seat, for which the person who stands i-th on the line would Buy the ticket. Sample Input Input3 4 61 11 11 11 21 31 3 Output1 11 22 11 31 42 3 Input4 3 122 22 22 22 22 22 22 22 22 22 22 22 2 Output2 21 22 12 33 21 11 33 13 34 24 14 3
|x1-x2|+|y1-y2| You can understand how many steps to walk, vertical walk How many steps, add up is its value, now the main thinking is, from a point to choose, probably the same path has been done once, in order to prevent timeouts, our main work is to reduce the same path repeated several times, so through a d[x][y] Indicates how many times around this point has been gone here we have to consider a situation, that is, to surround the situation, if the location of a point at the time of the other point is already visited, then we have to update the point by another point now explain the meaning of this code: for (int i=-1; i<=1; i++) {for (int j=-1; j<=1; J + +) {if (x+i<=0| | y+j<=0| | x+i>=n| | Y+J>=M) continue; D[x][y]=max (D[x][y],d[x+i][y+j]-abs (i)-abs (j)); }}d[x][y] has been explained before, representing from this point around the beginning of the d-1 steps have been selected, not empty position so for d[x][y] and d[x+i][y+j], you can draw a picture, or in the mind to think about
If D[X+I][Y+J] is zero, prove d[x][y] and D[x+i][y+j] is no intersection so get the affirmation or d[x][y] because d[x+i][y+j]-abs (i)-abs (j), this represents (X+I,Y+J) this point to (x, y) Distance, this formula must be less than 0 and then we think if D[X+I][Y+J] from this point around (x+i,y+j) from the beginning of the D-1 step has been selected, and (x, y) This point is also included in the d-1 step, then we can think about, if this point has been visited, Is it possible to get the D value of the minimum number of steps I should take with the D value of the other point, yes, because if D[X+I][D+J] step includes (x, y) This point is accessed, then the value of the d[x+i][y+j] value of the point minus ABS (i) +abs (j) is his The maximum radius of a point has been accessed around (x, y)
So take the maximum of all cases, here I and J range can be self-drawn, because through the above description of his value can be eligible for any value, but for the time complexity of the consideration you can choose ( -1,1), or ( -2,2), or ( -3,3) can be.
Code submission:
/*author:2486memory:19488 kbtime:795 mslanguage:gnu g++ 4.9.2result:accepted*/#include <cstdio> #include <cs tring> #include <cmath> #include <algorithm>using namespace Std;const int Maxn=2000+5;bool vis[maxn][ Maxn];int D[maxn][maxn];int n,m,k,ex,ey;//The function is to convert D to rows and columns for inspection, code everyone understands bool check (int x,int y) {int L=max (x-d[x][y],1), R =min (X+d[x][y],n); for (int i=l; i<=r; i++) {int t=d[x][y]-abs (i-x); if (Y-t>0&&!vis[i][y-t]) {ex=i; Ey=y-t; return false; } if (Y+t<=m&&!vis[i][y+t]) {ex=i; Ey=y+t; return false; }} return true; int main () {//freopen ("D://imput.txt", "R", stdin); int x, y; scanf ("%d%d%d", &n,&m,&k); for (int f=0; f<k; f++) {scanf ("%d%d", &x,&y); if (!vis[x][y]) {printf ("%d%d\n", x, y); Vis[x][y]=true; Continue } for (int i=-1; i≪=1; i++) {for (int j=-1; j<=1; J + +) {if (x+i<=0| | y+j<=0| | x+i>=n| | Y+J>=M) continue; D[x][y]=max (D[x][y],d[x+i][y+j]-abs (i)-abs (j)); }} while (Check (x, y)) {//And then step-by-step outward expansion d[x][y]++; } vis[ex][ey]=true; printf ("%d%d\n", Ex,ey); } return 0;}
|