目录
1.Problem
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,CA,B,C listed in order, the angle between these three points, centered at BB, is strictly less than 9090 degrees.
Points A,B,CA,B,C on the left have angle less than 9090 degrees, so they can be three consecutive points of a nice beatmap; Points A′,B′,C′A′,B′,C′ on the right have angle greater or equal to 9090 degrees, so they cannot be three consecutive points of a nice beatmap.
Now Nezzar has a beatmap of nn distinct points A1,A2,…,AnA1,A2,…,An. Nezzar would like to reorder these nn points so that the resulting beatmap is nice.
Formally, you are required to find a permutation p1,p2,…,pnp1,p2,…,pn of integers from 11 to nn, such that beatmap Ap1,Ap2,…,ApnAp1,Ap2,…,Apn is nice. If it is impossible, you should determine it.
2.Input
The first line contains a single integer nn (3≤n≤50003≤n≤5000).
Then nn lines follow, ii-th of them contains two integers xixi, yiyi (−109≤xi,yi≤109−109≤xi,yi≤109) — coordinates of point AiAi.
It is guaranteed that all points are distinct.
3.Output
If there is no solution, print −1−1.
Otherwise, print nn integers, representing a valid permutation pp.
If there are multiple possible answers, you can print any.
4.Examples
4.1input
5
0 0
5 0
4 2
2 1
3 0
4.2output
1 2 5 3 4
5.Code
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; }
template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; }
int readint() {
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;
ll dis[5005][5005];
pll a[5005];
bool vis[5005];
ll sqr(ll x) { return x * x; }
int main() {
n = readint();
for(int i = 1; i <= n; i++)
a[i].fi = readint(), a[i].se = readint();
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++)
dis[i][j] = dis[j][i] = sqr(a[i].fi - a[j].fi) + sqr(a[i].se - a[j].se);
printf("%d ", 1);
vis[1] = 1;
int now = 1;
for(int i = 2; i <= n; i++) {
ll maxa = 0, opt = 0;
for(int j = 1; j <= n; j++)
if(!vis[j] && chkmax(maxa, dis[now][j]))
opt = j;
printf("%d ", opt);
vis[opt] = 1;
now = opt;
}
printf("\n");
return 0;
}
6.Conclusion
这段代码是一个求解点集中点到点的距离并输出遍历序列的程序。具体来说:
1.通过 readint 函数读取输入的整数 n,表示点的个数。
2.通过循环读取每个点的坐标,并计算任意两点之间的距离,将结果保存在二维数组 dis 中。
3.以第一个点为起点,通过贪心算法选择下一个未访问过的点,该点到当前点的距离最大。将选择的点加入遍历序列,并标记为已访问。
4.输出遍历序列。总体而言,这段代码的作用是从给定的点集出发,通过贪心算法选择下一个距离最远的点,形成一个遍历序列。