E - Points on Plane(想法题)

原题:codeforces

题意:

给n个点,求一条走过所有点的路,使其曼哈顿距离小于 25e8

解析:

题目让我们求的不是最小路径,而是让我们想一种方法使其无论给什么数据都不会爆

我们对x坐标分块,k=sqrt(1e6),然后对一块区域上,下一块区域下,总距离好比莫队算法的时间复杂度

代码:


D read(){ D ans=0; char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans; return ans;
}
const int N=100009;
int k=(int)sqrt(1000000);
struct node {
    int x,y,id;
    bool operator<(const node a)const{
        if(x/k!=a.x/k)return x/k<a.x/k;
        if((x/k)%2)return y>a.y;
        return y<a.y;
    }
}e[1000009];

int main(){
    int n=read();
    for(int i=1;i<=n;i++){
        scanf("%d%d",&e[i].x,&e[i].y);e[i].id=i;
    }
    sort(e+1,e+1+n);
    for(int i=1;i<=n;i++){
        printf("%d ",e[i].id);
    }printf("\n");
}

猜你喜欢

转载自blog.csdn.net/jk_chen_acmer/article/details/81354819
今日推荐