标签:贪心 cout pair name 链接 max scan ace cto
有 n 个点,求一个排列,要求从中任选三个点,它们的夹角小于 90度。
贪心,以 1 号点为起始点,寻找距离 1 号点最远的点,加入序列,再找到和新加入序列的点距离最远的点,如此往复。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pll pair<long long, long long>
#define x first
#define y second
pll operator-(pll a, pll b) { return {a.x - b.x, a.y - b.y}; }
ll dot(pll a, pll b) { return a.x * b.x + a.y * b.y; }
ll dist(pll a, pll b) { return dot(b - a, b - a); }
int main() {
int n;
scanf("%d", &n);
vector<pll> v(n);
for (int i = 0; i < n; i++) {
pll p;
scanf("%lld%lld", &p.x, &p.y);
v[i] = p;
}
int c = 0;
vector<int> res;
vector<bool> st(n, 0);
res.push_back(0);
for (int i = 1; i < n; i++) {
ll maxx = 0;
pll t = v[c];
for (int j = 1; j < n; j++) {
if (st[j]) continue;
if (dist(t, v[j]) > maxx) {
maxx = dist(t, v[j]);
c = j;
}
}
st[c] = true;
res.push_back(c);
}
for (int i = 0; i < res.size(); i++) {
cout << res[i] + 1 << " ";
}
cout << endl;
return 0;
}
CF1478F Nezzar and Nice Beatmap
标签:贪心 cout pair name 链接 max scan ace cto
原文地址:https://www.cnblogs.com/ancode/p/14357877.html