C. Points on Plane
On a plane are n points (xi, yi) 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.
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.
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.
50 78 103 45 09 12
Sample Output
4 3 1 2 5
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个点的坐标(范围[0,10^6]),求一种连边形成链后总长度<=2.5*10^9 的方案。n<=10^6。链的长度是链上相邻两点的曼哈顿距离和。(输出任意解即可)
题解:将X轴的点分成1000块,这样每块内Y坐标可走10^6,X坐标可走10^3,每块之间的连接最长为2*10^3,所以最大总距离为2*10^9+2*10^6,符合要求。
AC代码:
#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n; const int maxn = 1000007; pair<int,pair<int,int> > Q[maxn]; int main() { n = read(); for(int i=1;i<=n;i++) { Q[i].first = read()/1050; Q[i].second.first = read(); Q[i].second.second = i; } sort(Q+1,Q+1+n); for(int i=1;i<=n;i++) { cout<<Q[i].second.second<<" "; } cout<<endl; return 0; }