Codeforces 576C. Points on Plane(构造)

时间:2023-03-10 05:30:32
Codeforces 576C. Points on Plane(构造)

  将点先按x轴排序,把矩形竖着划分成$10^3$个块,每个块内点按y轴排序,然后蛇形走位上去。

  这样一个点到下一个点的横坐标最多跨越$10^3$,一共$10^6$个点,总共$10^9$,一个块内最多走$10^6$,一共$10^3$个块,一共$10^9$,跨过块的部分一共$2*10^6$,也就是总共不会超过$2*10^9+2*10^6$。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct poi{int x, y, pos;}a[maxn];
int n, x;
void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline bool cmp1(poi a, poi b){return a.x<b.x;}
inline bool cmp2(poi a, poi b){return a.y<b.y;}
int main()
{
read(n);
for(int i=;i<=n;i++) read(a[i].x), read(a[i].y), a[i].pos=i;
sort(a+, a++n, cmp1); int now=;
for(int i=;i<=;i++)
{
x=i*;
int l=now, r=now;
for(;a[r].x<=x && r<=n;r++); r--;
if(l>r) continue;
sort(a++l, a++r, cmp2); now=r+;
if(i&) for(int j=l;j<=r;j++) printf("%d ", a[j].pos);
else for(int j=r;j>=l;j--) printf("%d ", a[j].pos);
}
}