Codeforces Round #319 (Div. 2)E Points on Plane

来源:互联网 发布:数据库地址 编辑:程序博客网 时间:2024/04/28 15:35
题意:给出n个点,要求排序后,相邻两点的欧拉距离之和小于等于2.5e9
做法:由于0≤ xi, yi ≤ 1e6,所以可以将x<=1000的点分成一份,1000<x<=2000的点分成第二份,以此类推,分成一千份。
然后每一份中的点都按照y单调排序。拿任意一份点做实验,如果从最小的y开始往上走,那么y的贡献最多1e6,那么一千份就总共最多贡献1e9。

最后考虑x的贡献,在某一份点中,从一个点走到另一个点最多贡献1e3,那么这份总共最多贡献1e9,也就是所有点都在这一份里面,那么考虑所有点集,那x总共贡献1e9。如果分散在其它点集中,那么总共贡献大概想想也是1e9。加起来是2e9,必然满足要求。

所以,排个序就可以做了。


#include<bits\stdc++.h>using namespace std;struct point{int id,x,y;bool operator < (point a)const{return y<a.y;}};vector<point> p[1005];int main(){    int n;cin >> n;for(int i = 1;i<=n;i++){point t;scanf("%d%d",&t.x,&t.y);t.id = i;p[t.x/1000].push_back(t);}int flag = 0;for(int i = 0;i<=1000;i++){sort(p[i].begin(),p[i].end());if(p[i].size()){int len = p[i].size();if(!flag)for(int j = 0;j<len;j++)printf("%d ",p[i][j].id);else    for(int j = len-1;j>=0;j--)printf("%d ",p[i][j].id);flag^=1;}}}


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 bis 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
50 78 103 45 09 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





0 0
原创粉丝点击