poj 3714 Raid(平面最近点对)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 7473 |
Accepted: 2221 |
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 i
ntegers 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
Source
1对原数组依据x左标从小到大排序。
2二分数组,左边求出最小值,右边求出最小值,我们求最小的。
3找出对于左右两边的可能小于当前最小值的最近点对,更新最小值。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring> #define DX(x) ((x) * (x))
using namespace std; const int MAX = + ;
const double INF = 10e100;
struct Point
{
double x, y;
int index,flag;//flag分类!!
}A[MAX], B[MAX], C[MAX]; bool compX(const Point& a, const Point& b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
} bool compY(const Point& a, const Point& b)
{
if(a.y == b.y)
return a.x < b.x;
return a.y < b.y;
} inline double getMin(double a, double b)
{
return a < b ? a : b;
} double getDist(Point& a, Point& b)
{
if(a.flag==b.flag)return INF;//同类规定距离为oo!!
return sqrt(DX(a.x - b.x) + DX(a.y - b.y));
} void merge(Point p[], Point q[], int s, int m, int t)
{
int i, j, k;
for(i = s, j = m + , k = s; i <= m && j <=t;)
{
if(q[i].y > q[j].y)
p[k++] = q[j], j++;
else
p[k++] = q[i], i++;
}
while(i <= m)
p[k++] = q[i++];
while(j <= t)
p[k++] = q[j++];
} double closest(Point a[], Point b[], Point c[], int p, int q)
{
if(q - p == )
return getDist(a[p], a[q]);
if(q - p == )
{
double x1 = getDist(a[p], a[q]);
double x2 = getDist(a[p + ], a[q]);
double x3 = getDist(a[p], a[p + ]);
return getMin(x1, getMin(x2, x3));
}
int i, j, k, m = (p + q) / ;
double d1, d2;
for(i = p, j = p, k = m + ; i <= q; ++i)
{
if(b[i].index <= m)
c[j++] = b[i];
else
c[k++] = b[i];
}
d1 = closest(a, c, b, p, m);
d2 = closest(a, c, b, m + , q);
double dm = getMin(d1, d2);
merge(b, c, p, m, q);
for(i = p, k = p; i <= q; ++i)
{
if(fabs(b[i].x - b[m].x) < dm)
c[k++] = b[i];
}
for(i = p; i < k; ++i)
for(j = i + ; j < k && c[j].y - c[i].y < dm; ++j)
{
double temp = getDist(c[i], c[j]);
if(temp < dm)
dm = temp;
}
return dm;
}
int main()
{
//freopen("in.txt", "r", stdin);
int T,n;
scanf("%d",&T);
while( T--)
{
scanf("%d", &n);
for(int i = ; i < n; ++i)
scanf("%lf%lf", &A[i].x, &A[i].y),A[i].flag=;
for(int i = ; i < n; ++i)
scanf("%lf%lf", &A[i+n].x, &A[i+n].y),A[i+n].flag=;
sort(A, A + *n, compX);
for(int i = ; i < *n; ++i)
A[i].index = i;
memcpy(B, A, *n * sizeof(B[]));
sort(B, B + *n, compY);
double d = closest(A, B, C, , *n - );
printf("%.3f\n", d);
}
return ;
}
poj 3714 Raid(平面最近点对)的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- POJ 3714 Raid(计算几何の最近点对)
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- POJ 3714 Raid(平面近期点对)
解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- POJ-3714 Raid 平面最近点对
题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...
- POJ 3714 Raid 近期对点题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714
这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...
随机推荐
- 监听浏览器返回键、后退、上一页事件(popstate)操作返回键
在WebApp或浏览器中,会有点击返回.后退.上一页等按钮实现自己的关闭页面.调整到指定页面.确认离开页面或执行一些其它操作的需求.可以使用 popstate 事件进行监听返回.后退.上一页操作. 一 ...
- open jdk卸载
//查找:open jdk # rpm -qa | grep java //卸载open jdk # rpm -e --nodeps 包 # source /etc/profile # java -v ...
- webrtp官方demo运行
Google官方提供的webrtc的demo对应的网站是https://webrtc.github.io/samples/ 上面的DEMO对我这种入门的人很有用,用chrome浏览器最新的版本直接可以 ...
- maven之阿里云Maven镜像的使用
Maven中央仓库在国外,速度比较慢,所以我们采用国内的镜像,速度回有质的提升. 配置下setting.xml <mirrors> <mirror> <id>ali ...
- FireMonkey 绘图(1)
FireMonkey 绘图(1) FMX 的 Canvas 在不同的系统上会分别使用:WinVista.Win7: D2D (FMX.Canvas.D2D.pas)WinXP: GDI+ (FMX.C ...
- 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_2 Mybatis中编写dao实现类的使用-保存操作
再完善.saveUser的方法 测试保存的操作 报错了 SqlSession的insert的源码 我们在执行Insert的时候,并没有把user对象传过去 usersex改成sex 再次测试
- python string_1
quote :http://www.runoob.com/python/python-strings.html #coding:utf-8 s1="http://www.jnshu.com/ ...
- 【Python】利用豆瓣短评数据生成词云
在之前的文章中,我们获得了豆瓣爬取的短评内容,汇总到了一个文件中,但是,没有被利用起来的数据是没有意义的. 前文提到,有一篇微信推文的关于词云制作的一个实践记录,准备照此试验一下. 思路分析 读文件 ...
- kafka学习(四)
集群成员关系 kafka使用Zookeeper 来维护集群成员的信息.每个broker都有一个唯一标识符,这个标识符可以在配置里指定,也可以自动生成.在broker启动的时候,它通过创建临时节点把自己 ...
- shell脚本每隔几秒执行
while true do cmd(shell 命令) sleep x(x为秒数) done ————————————————版权声明:本文为CSDN博主「这年头起名真难3232」的原创文章,遵循 C ...