F. Nezzar and Nice Beatmap

F. Nezzar and Nice Beatmap
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Nezzar loves the game osu!.

osu! is played on beatmaps, which can be seen as an array consisting of distinct points on a plane. A beatmap is called nice if for any three consecutive points A,B,C listed in order, the angle between these three points, centered at B, is strictly less than 90 degrees.

Points A,B,C on the left have angle less than 90 degrees, so they can be three consecutive points of a nice beatmap; Points A′,B′,C′ on the right have angle greater or equal to 90 degrees, so they cannot be three consecutive points of a nice beatmap.
Now Nezzar has a beatmap of n distinct points A1,A2,…,An. Nezzar would like to reorder these n points so that the resulting beatmap is nice.

Formally, you are required to find a permutation p1,p2,…,pn of integers from 1 to n, such that beatmap Ap1,Ap2,…,Apn is nice. If it is impossible, you should determine it.

Input
The first line contains a single integer n (3≤n≤5000).

Then n lines follow, i-th of them contains two integers xi, yi (−109≤xi,yi≤109) — coordinates of point Ai.

It is guaranteed that all points are distinct.

Output
If there is no solution, print −1.

Otherwise, print n integers, representing a valid permutation p.

If there are multiple possible answers, you can print any.

Example
inputCopy
5
0 0
5 0
4 2
2 1
3 0
outputCopy
1 2 5 3 4

Please note that the angle between A1, A2 and A5, centered at A2, is treated as 0 degrees. However, angle between A1, A5 and A2, centered at A5, is treated as 180 degrees.

当Ai,Ai+1,Ai+2没有形成锐角时,Ai+2与Ai+2交换,类似冒泡排序判断或交换所有点

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 100000
struct point
{
	ll x,y,id;
}p[N];
ll n;
ll calc(ll x1,ll y1,ll x2,ll y2)
{
	return x1*x2+y1*y2;
}
int main()
{
	scanf("%lld",&n);
	for (int i=1;i<=n;i++) 
	{
		scanf("%lld%lld",&p[i].x,&p[i].y);
		p[i].id=i;
	}
	for (int i=3;i<=n;i++)
		for (int j=i;j>=3;j--)
			if (calc(p[j].x-p[j-1].x,p[j].y-p[j-1].y,
					 p[j-2].x-p[j-1].x,p[j-2].y-p[j-1].y)<=0)
				swap(p[j],p[j-1]);
			else break;
	for (int i=1;i<=n;i++) printf("%lld ",p[i].id);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值