Educational Codeforces Round 124 (Rated for Div. 2)
阿新 • • 發佈:2022-03-13
比賽連結
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\)
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 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\)
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;
}