Codeforces Round #319 (Div. 2) E. Points on Plane 莫队+优化

E. Points on Plane

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

On a plane are n points (xiyi) with integer coordinates between 0 and 106. The distance between the two points with numbers a and b is said to be the following value:  (the distance calculated by such formula is called Manhattan distance).

We call a hamiltonian path to be some permutation pi of numbers from 1 to n. We say that the length of this path is value .

Find some hamiltonian path with a length of no more than 25 × 108. Note that you do not have to minimize the path length.

Input

The first line contains integer n (1 ≤ n ≤ 106).

The i + 1-th line contains the coordinates of the i-th point: xi and yi (0 ≤ xi, yi ≤ 106).

It is guaranteed that no two points coincide.

Output

Print the permutation of numbers pi from 1 to n — the sought Hamiltonian path. The permutation must meet the inequality .

If there are multiple possible answers, print any of them.

It is guaranteed that the answer exists.

Examples

input

5
0 7
8 10
3 4
5 0
9 12

output

4 3 1 2 5 

Note

In the sample test the total distance is:

(|5 - 3| + |0 - 4|) + (|3 - 0| + |4 - 7|) + (|0 - 8| + |7 - 10|) + (|8 - 9| + |10 - 12|) = 2 + 4 + 3 + 3 + 8 + 3 + 1 + 2 = 26


题意:

       给你二维平面上的n个坐标,要你经过所有的点,但是总路程小于25*1e8。(按顺序的两个坐标的路程就是两坐标的距离)

输出按下标标号的经过顺序。

做法:

       一开始看到题面可能会有点懵,只要总路程小于25*1e8就好,但是这道题有100w个点,点的坐标范围也是在(0,0)到(1e6,1e6)的,所以如果样例比较极端,再随便连的话超过25*1e8是分分钟的事情,所以要进行优化。

       因为连点肯定是离得越近越好,一个区域连过之后就不要再回来了,按照类似从左到右的顺序连边。因此就用到了莫队的分块思想,这里有个莫队的优化,如果分到的块是奇数块,那y坐标从小到大,偶数块从大到小,这样路线就会变成

可以达到我们的要求。

代码很短如下:


#include<bits/stdc++.h>
using namespace std;
struct node{
    int x,y,lev,id;
    bool operator < (const node &a) const {
        if(lev==a.lev) {
            if(lev%2) return y<a.y;
            return y>a.y;
        }
        return lev<a.lev;
    }
}e[1000005];
int n,biao;
int main(){
    scanf("%d",&n);
    biao=sqrt(n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&e[i].x,&e[i].y);
        e[i].id=i,e[i].lev=e[i].x/biao;
    }
    sort(e+1,e+1+n);
    for(int i=1;i<=n;i++){
        printf("%d%c",e[i].id,i==n?'\n':' ');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81626065
今日推荐