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 (xi,yi).
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 (x1,y1) and (x2,y2) is |x1x2|+|y1y2|.

Input

The first line of the input contains one integer n(1n2105) - the number of points in the set.
The next n lines describe points. The i-th of them contains two integers xi and yi(1xi,yi2105) 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 [106;106]. 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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15995380.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示