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

本文介绍了一种解决平面点排序问题的算法,通过将点按x坐标区间分组并内部按y坐标排序的方法,确保了任意两点间的欧拉距离之和不超过2.5e9。适用于大规模数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给出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
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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值