Codeforces Round #319 (Div. 1) C. Points on Plane

                                C. Points on Plane

题目连接

http://codeforces.com/contest/576/problem/C

Description

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.

Sample Input

5
0 7
8 10
3 4
5 0
9 12

Sample Output

4 3 1 2 5 

HINT

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值