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=1000010;
struct poi{int x, y, pos;}a[maxn];
int n, x;
void read(int &k)
{
    int f=1; k=0; char c=getchar();
    while(c<'0' || c>'9') c=='-' && (f=-1), c=getchar();
    while(c<='9' && c>='0') k=k*10+c-'0', 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=1;i<=n;i++) read(a[i].x), read(a[i].y), a[i].pos=i;
    sort(a+1, a+1+n, cmp1); int now=1;
    for(int i=1;i<=1000;i++)
    {
        x=i*1000;
        int l=now, r=now;
        for(;a[r].x<=x && r<=n;r++); r--;
        if(l>r) continue;
        sort(a+1+l, a+1+r, cmp2); now=r+1;
        if(i&1) 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);
    }
}
View Code
原文地址:https://www.cnblogs.com/Sakits/p/8031077.html