[CF598C] Nearest vectors - 计算几何
Description
有 n 个点,每个点表示原点到该点的向量,让你求出两个向量最小的夹角,输出向量的序号。
Solution
考虑暴力算出极角然后排序,用 atan2(y,x)
#include <bits/stdc++.h>
using namespace std;
#define int long long
const double pi = acos(-1);
signed main()
{
struct point
{
long double x, y, a;
int id;
void gen_angle()
{
long double res = atan2(y, x);
if (res < 0.0)
res += 2 * pi;
a = res;
}
long double angle() const
{
return a;
}
bool operator<(const point &rhs) const
{
return angle() < rhs.angle();
}
};
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<point> p(n + 2);
for (int i = 1; i <= n; i++)
cin >> p[i].x >> p[i].y, p[i].gen_angle(), p[i].id = i;
sort(&p[1], &p[n + 1]);
p[n + 1] = p[1];
long double min_value = 1e9;
int min_pos = 0;
for (int i = 1; i < n; i++)
{
long double now = abs(p[i + 1].angle() - p[i].angle());
if (now < min_value)
{
min_value = now;
min_pos = i;
}
}
long double now = abs(-p[n].angle() + 2 * pi + p[1].angle());
if (now < min_value)
{
min_value = now;
min_pos = n;
}
int x = min_pos, y = min_pos + 1;
cout << p[x].id << " " << p[y].id << endl;
}