Educational Codeforces Round 124 (Rated for Div. 2)

比赛链接

Educational Codeforces Round 124 (Rated for Div. 2)

D. Nearest Excluded Points

You are given \(n\) distinct points on a plane. The coordinates of the \(i\)-th point are \(\left(x_{i}, y_{i}\right)\).
For each point \(i\), find the nearest (in terms of Manhattan distance) point with integer coordinates that is not among the given \(n\) points. If there are multiple such points - you can choose any of them.
The Manhattan distance between two points \(\left(x_{1}, y_{1}\right)\) and \(\left(x_{2}, y_{2}\right)\) is \(\left|x_{1}-x_{2}\right|+\left|y_{1}-y_{2}\right|\).

Input

The first line of the input contains one integer \(n\left(1 \leq n \leq 2 \cdot 10^{5}\right)\) - the number of points in the set.
The next \(n\) lines describe points. The \(i\)-th of them contains two integers \(x_{i}\) and \(y_{i}\left(1 \leq x_{i}, y_{i} \leq 2 \cdot 10^{5}\right)-\) coordinates of the \(i\)-th point. It is guaranteed that all points in the input are distinct.

Output

Print \(n\) lines. In the \(i\)-th line, print the point with integer coordinates that is not among the given \(n\) points and is the nearest (in terms of Manhattan distance) to the \(i\)-th point from the input.
Output coordinates should be in range \(\left[-10^{6} ; 10^{6}\right]\). It can be shown that any optimal answer meets these constraints.
If there are several answers, you can print any of them.

Examples

input

6
2 2
1 2
2 1
3 2
2 3
5 5

output

1 1
1 1
2 0
3 1
2 4
5 4

input

8
4 4
2 4
2 2
2 3
1 4
4 2
1 3
3 3

output

4 3
2 5
2 1
2 5
1 5
4 1
1 2
3 2

解题思路

bfs

先把那些哈密顿距离为 \(1\) ,即距离家最近且点集中不存在的点找出来的,则剩余的那些点一定在这些点的附近,否则也能找到不能存在点集且两点间的哈密顿距离为 \(1\) 的点,将之前那些已经找到答案的点每次向外扩张,即多源 \(bfs\),找跟其距离最近且没有答案的点,找到最短的那个点,源点的答案即为找到的那个点的答案,因为其哈密顿距离最小为最短路加一

  • 时间复杂度:\(O(n)\)

代码

// Problem: D. Nearest Excluded Points
// Contest: Codeforces - Educational Codeforces Round 124 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1651/problem/D
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e5+5;
int n,x[N],y[N],dx[]={-1,0,1,0},dy[]={0,1,0,-1};
map<PII,int> mp;
PII res[N];
bool vis[N];
queue<int> q;
void bfs()
{
	while(q.size())
	{
		int u=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int _x=x[u]+dx[i],_y=y[u]+dy[i];
			if(mp.count({_x,_y}))
			{
				int v=mp[{_x,_y}];
				if(!vis[v])
				{
					vis[v]=true;
					res[v]=res[u];
					q.push(v);
				}
			}
		}
	}
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>x[i]>>y[i];
    	mp[{x[i],y[i]}]=i;
    }
    for(int i=1;i<=n;i++)
	    for(int j=0;j<4;j++)
	    {
	    	int _x=x[i]+dx[j],_y=y[i]+dy[j];
	    	if(!mp.count({_x,_y}))
	    	{
	    		res[i]={_x,_y};
	    		vis[i]=true;
	    		q.push(i);
	    	}
	    }
    bfs();
    for(int i=1;i<=n;i++)cout<<res[i].fi<<' '<<res[i].se<<'\n';
    return 0;
}
posted @ 2022-03-11 21:10  zyy2001  阅读(41)  评论(0编辑  收藏  举报