poj 3714Raid(最近点对)
来源:互联网 发布:it维修单 编辑:程序博客网 时间:2024/05/16 00:33
Raid
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9575 Accepted: 2919
Description
After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.
The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?
Input
The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.
Output
For each test case output the minimum distance with precision of three decimal placed in a separate line.
Sample Input
2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Sample Output
1.414
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 9575 Accepted: 2919
Description
After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.
The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?
Input
The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.
Output
For each test case output the minimum distance with precision of three decimal placed in a separate line.
Sample Input
2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Sample Output
1.414
0.000
solution:
将n个点按x坐标进行排序,分为左右两半,分别求出两半内最近点对的距离,取最小值作为当前的答案,设为ans。然后考虑把两个点集合并,在分割线处还有可能会有更优的答案,去分割线处左右ans距离内的点,按y坐标排序,用每个点与其左右6个点的距离更新答案即可。
PS:注意所求最近距离不是同一方的最短距离
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int maxn = 1e5 + 200;const double eps = 1e-8;int n, s[maxn<<1];int cmp(double x){if (fabs(x) < eps)return 0;else if (x > 0)return 1;return -1;}double sqr(double x){return x*x;}struct point {double x, y;int f;point(){ }point(double a, double b) :x(a), y(b){}friend point operator - (const point &a, const point &b){return point(a.x - b.x, a.y - b.y);}double norm(){return sqrt(sqr(x) + sqr(y));}}a[maxn<<1];bool cmpx(int i, int j){return cmp(a[i].x - a[j].x) < 0;}bool cmpy(int i, int j){return cmp(a[i].y - a[j].y) < 0;}double min_dist(int l, int r){double ans = 1e100;if (r - l < 20){for (int q = l; q < r; q++)for (int w = q + 1; w < r; w++)if (a[s[q]].f != a[s[w]].f)ans = min(ans, (a[s[q]] - a[s[w]]).norm());return ans;}int tl, tr, m = (l + r) >> 1;ans = min(min_dist(l, m), min_dist(m, r));for (tl = l; a[s[tl]].x < a[s[m]].x - ans; tl++);for (tr = r - 1; a[s[tr]].x > a[s[m]].x + ans; tr--);sort(s + tl, s + tr, cmpy);for (int q = tl; q < tr; q++)for (int w = q + 1; w < min(tr, q + 6); w++)if (a[s[q]].f != a[s[w]].f)ans = min(ans, (a[s[q]] - a[s[w]]).norm());sort(s + tl, s + tr, cmpx);return ans;}double solve(int n){for (int i = 0; i < n; i++)s[i] = i;sort(s, s + n, cmpx);return min_dist(0, n);}int main(){int t;scanf("%d", &t);while (t--){scanf("%d", &n);for (int i = 0; i < n; i++){scanf("%lf%lf", &a[i].x, &a[i].y);a[i].f = 0;}for (int i = 0; i < n; i++){scanf("%lf%lf", &a[i+n].x, &a[i+n].y);a[i+n].f = 1;}n <<= 1;printf("%.3lf\n", solve(n));}return 0;}
0 0
- POJ 3714 Raid 最近点对
- poj 3714 Raid(最近点对)
- POJ 3714 Raid 最近对点题解
- POJ 3714 Raid 平面最近点对
- POJ 3714 Raid 最近点对
- POJ 3714 Raid(最近点对)
- poj 3714Raid(最近点对)
- POJ 3714(Raid-平面最近点对)
- POJ 3714 Raid(最近点对)
- poj 3714 Raid (类型不同点间的最近点对)
- poj 3714 Raid(平面最近点对)
- POJ 3714 Raid 最近点对 扫描线
- poj 3714 Raid 分治法求平面最近点对
- POJ 3714 Raid(平面最近点对)
- POJ 3714 Raid(平面最近点对,不同类型点之间)
- POJ 3714 Raid(求两集合之间的最近点对)
- POJ 3714 Raid (最近点对)感觉数据怪怪的
- POJ 3714 Raid(变种最近点对问题:分治+剪枝)
- 行为型:设计模式之策略模式(十六)
- [oralce拾遗]oracle修改监听IP地址
- Storm Tutorial
- 查看程序占用端口
- iOS读写文件(NSFileManager)
- poj 3714Raid(最近点对)
- 继承中的this的含义及通过反射获取泛型参数
- throw Java(二)
- 读书笔记--android中的单例模式
- mysql 字段添加、删除、修改
- 灵异?奇异?《新天龙》小片子年夜联播
- 跳转过去黑屏
- InnoDB缓存池介绍
- linker command failed with exit code 1 (use -v to see invocation)