poj 3714 Raid
程序员文章站
2022-05-09 10:05:13
...
最近点对算法的变形(看别人写的)
两个集合可以放在一起,只要在在跨左右集合寻找最近点的时候判断一下是否都是agent或者都是pow station 就行了。(听说数据很水,我写的也很水)。
#include<iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int MAX = 1e6;
struct node
{
double x, y;
int sign;
};
node num[MAX] = { 0 };
int n;
bool cmp_x(node x, node y)
{
return x.x < y.x;
}
bool cmp_y(node x, node y)
{
return x.y < y.y;
}
double dis(node x, node y)
{
return sqrt(abs(x.y - y.y) * abs(x.y - y.y) + abs(x.x - y.x) * abs(x.x - y.x));
}
double solve(int lf,int rt)
{
if (lf == rt) return 9999999999;
if (lf + 1 == rt) if (num[lf].sign == num[rt].sign) return 999999999;else return dis(num[lf], num[rt]);
//if (lf + 2 == rt) return min(dis(num[lf], num[rt]), min(dis(num[lf],num[lf + 1]), dis(num[lf + 1],num[rt])));
int mid = ((lf + rt) >> 1);
double tmp = min(solve(lf, mid), solve(mid + 1, rt));//cout << " tmp " << tmp << endl;
vector<node> v;
for (int i = lf; i <= rt; i++)
{
if (num[i].x >= mid - tmp && num[i].x <= mid + tmp)
v.push_back(num[i]);
}
sort(v.begin(), v.end(), cmp_y);
double k = tmp;
for (int i = 0; i < v.size(); i++)
{
for (int j = 1; j < v.size() && abs(v[i].y-v[j].y) < k; j++)
{
if (i != j && v[i].sign != v[j].sign)
tmp = min(tmp, dis(v[i], v[j]));
}
}
return tmp;
}
int main()
{
int t; cin >> t;
while (t--)
{
cin >> n;
for (int i = 1; i <= n; i++) {
scanf_s("%lf%lf", &num[i].x, &num[i].y); num[i].sign = 1;
}
for (int i = n+1; i <= 2 * n; i++)
{
scanf_s("%lf%lf", &num[i].x, &num[i].y); num[i].sign = 2;
}
sort(num + 1, num + n * 2 + 1, cmp_x);
printf("%.3lf\n", solve(1,n * 2));
}
return 0;
}
下一篇: Luogu P1024题解