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 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 (xi, yi), where xiis the row number, and yi 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 (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 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 x2 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 y2 is minimum

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

Input

The first input line contains three integers nmk (1 ≤ n, m ≤ 20001 ≤ 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 next k lines contains two integers xiyi(1 ≤ xi ≤ n1 ≤ 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

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 in the line will buy the ticket.

Sample test(s)
input
3 4 6
1 1
1 1
1 1
1 2
1 3
1 3
output
1 1
1 2
2 1
1 3
1 4
2 3
input
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
1 2
2 1
2 3
3 2
1 1
1 3
3 1
3 3
4 2
4 1
4 3

题意:给定N*M的矩阵,然后依次给出K个理想的点的坐标x,y,但是,如果前面的点有在x,y处,那么就按照题意所给定的规则,将点放在理想点坐标(x,y)附近的某个位置。 nmk (1 ≤ n, m ≤ 20001 ≤ k ≤ min(n·m, 105

规则如下:

1.the value of |x1 - x2| + |y1 - y2| is minimum
2.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 minimum
3.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 FIN             freopen("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 fst             first
#define snd             second
/****************************>>>>>>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,暴力枚举)