http://www.elijahqi.win/2018/03/08/codeforces-576c-points-on-plane
题目描述
On a plane are
n
n points (
x_{i}
xi ,
y_{i}
yi ) with integer coordinates between
0
0 and
10^{6}
106 . The distance between the two points with numbers
a
a and
b
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
p_{i}
pi of numbers from
1
1 to
n
n . We say that the length of this path is value .
Find some hamiltonian path with a length of no more than
25×10^{8}
25×108 . Note that you do not have to minimize the path length.
输入输出格式
输入格式:
The first line contains integer
n
n (
1<=n<=10^{6}
1<=n<=106 ).
The
i+1
i+1 -th line contains the coordinates of the
i
i -th point:
x_{i}
xi and
y_{i}
yi (
0<=x_{i},y_{i}<=10^{6}
0<=xi,yi<=106 ).
It is guaranteed that no two points coincide.
输出格式:
Print the permutation of numbers
p_{i}
pi from
1
1 to
n
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.
输入输出样例
输入样例#1: 复制
5
0 7
8 10
3 4
5 0
9 12
输出样例#1: 复制
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
(∣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
更多的算思路题
求将所有点按照一个顺序排序使得所有相邻点之间的哈密顿距离相加<=2.5e9
将x按照每1000一分组 然后组内y按照升序排序考虑 y就算从小走到大一共只有1e6 那么10^3*1e6=1e9
坐标范围
考虑给x按照每1000个一分组 那么显然一共 10^3次组 每个组里的点最多走1000 那么1000^3正好是1e9
所以1e9+1e9<2.5e9
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 1100000
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x*f;
}
struct node{
int x,y,id;
}p[N];
inline bool cmp(const node &a,const node &b){
return a.x<b.x;
}
inline bool cmp1(const node &a,const node &b){
return a.y<b.y;
}
int n,nn;
vector<node> a[1100];
int main(){
// freopen("cf576c.in","r",stdin);
n=read();nn=1000;int mx=0;
for (int i=1;i<=n;++i) p[i].x=read(),p[i].y=read(),p[i].id=i,mx=max(mx,p[i].x);
sort(p+1,p+n+1,cmp);
for (int i=1;i<=n;++i) a[(p[i].x-1)/1000+1].push_back(p[i]);
for (int i=1;i<=1088;++i){
sort(a[i].begin(),a[i].end(),cmp1);
for (int j=0;j<a[i].size();++j) printf("%d ",a[i][j].id);
}
return 0;
}