codeforce #126Div2 200A Cinema 【优化暴力枚举】

A. Cinema

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided intonrows, each row consists ofmseats.

There arekpeople 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(xi,yi), wherexiis the row number, andyiis 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(x1,y1), then when he comes to the box office, he chooses such empty seat(x2,y2), which satisfies the following conditions:

the value of|x1-x2|+|y1-y2|is minimumif the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value ofx2is minimumif 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 ofy2is 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,105) — 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 nextklines contains two integersxi,yi(1≤xi≤n,1≤yi≤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

Printklines, each containing a pair of integers. Print on thei-th linexi,yi— the coordinates of the seat, for which the person who standsi-th in the line will buy the ticket.

Sample test(s)

input

3 4 61 11 11 11 21 31 3

output

1 11 22 11 31 42 3

input

4 3 122 22 22 22 22 22 22 22 22 22 22 22 2

output

2 21 22 12 33 21 11 33 13 34 24 14 3

)

规则如下:1.the value of |x1-x2|+|y1-y2| is minimum2.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 x2 is minimum3.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 y2 is minimum

分析:刚开始拿到这个题目,从给定的点(x1,y1)开始暴力地向外搜索没有被标记过的顶点,但是,显然,这样必定会TLE。我们大多都能想到单纯一个vis数组标记点(x,,y)有没有被访问还不行,而且由|x1-x2|+|y1-y2|,基本都能想到用一个dist数组来记录当前(x,y)多少范围之内的数已经被标记了,那么我枚举的时候,那些距离小于dis[x][y]的点我就不用再进行枚举了。这里的dis[x][y]可以类似圆的半径的概念吧~~~另外,我们更新dis[x][y]的时候,还有一个地方也要注意优化,就是在新加入(x,y)这一点时,【PS:要知道此时dis[x][y]可是为1或者是0的哦~】,如果(x,y)附近的某个点已经被频繁访问过很多遍了,也就是说,这个时候,我就不需要d = dis[x][y]开始找了,而是可以从dis[x + i][y + j] – abs(i) – abs(j) 开始找。有了这两个优化,这道题也就AC了~

/****************************>>>>HEADFILES<<<<****************************/#include <queue>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;/****************************>>>>>DEFINE<<<<<*****************************///#pragma comment(linker, "/STACK:1024000000,1024000000")#define FINfreopen("input.txt","r",stdin)#define rep(i,a,b)for(int i = a;i <= b;i++)#define MP(a,b)make_pair(a,b)#define PB(a)push_back(a)#define fstfirst#define sndsecond/****************************>>>>>>DEBUG<<<<<<****************************/#define out(x)cout<<x<<" ";/****************************>>>>SEPARATOR<<<<****************************/const int maxn = 2000 + 5;int N, M, K, dis[maxn][maxn];;bool vis[maxn][maxn];void f(int x, int y){for(int i = -1; i <= 1; i++)for(int j = -1; j <= 1; j++)if(x + i > 0 && x + i <= N && y + j > 0 && y + j <= M)dis[x][y] = max(dis[x + i][y + j] – abs(i) – abs(j), dis[x][y]);}int main(){// FIN;while(~scanf("%d %d %d", &N, &M, &K)){memset(vis, false, sizeof(vis));memset(dis, 0, sizeof(dis));int x, y;for(int i = 0; i < K; i++){scanf("%d %d", &x, &y);if(!vis[x][y]){printf("%d %d\n", x, y);vis[x][y] = true;dis[x][y] = 1;continue;}else{f(x, y); //这一句不能少,也是一个优化。bool suc = false;for(int &d = dis[x][y];; dis[x][y]++){for(int xx = x – d, y1 = 0; xx <= x; xx++, y1++){if(xx <= 0) continue;if(y – y1 > 0 && !vis[xx][y – y1]){suc = vis[xx][y – y1] = true;dis[xx][y – y1] = 1;printf("%d %d\n", xx, y – y1);break;}else if(y + y1 <= M && !vis[xx][y + y1]){suc = vis[xx][y + y1] = true;dis[xx][y + y1] = 1;printf("%d %d\n", xx, y + y1);break;}}if(suc) break;for(int xx = x, y1 = d; xx <= d + x; xx++, y1–){if(xx > N) break;if(y – y1 > 0 && !vis[xx][y – y1]){suc = vis[xx][y – y1] = true;dis[xx][y – y1] = 1;printf("%d %d\n", xx, y – y1);break;}else if(y + y1 <= M && !vis[xx][y + y1]){suc = vis[xx][y + y1] = true;dis[xx][y + y1] = 1;printf("%d %d\n", xx, y + y1);break;}}if(suc) break;}}}// rep(i,1,N) rep(j,1,M) printf("%d%c",dis[i][j],j == M?'\n':' ');}return 0;}

版权声明:本文为博主原创文章,未经博主允许不得转载。

一旦有了意志,脚步也会轻松起来。

codeforce #126Div2 200A Cinema 【优化暴力枚举】

相关文章:

你感兴趣的文章:

标签云: