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 ...
随机推荐
- hibernate缓存机制与N+1问题
在项目中遇到的趣事 本文基于hibernate缓存机制与N+1问题展开思考, 先介绍何为N+1问题 再hibernate中用list()获得对象: /** * 此时会发出一条sql,将30个学生全部查 ...
- 测开之路一百四十三:ORM框架之SQLAlchemy模型及表创建
基于前一篇内容,可以使用模型的结构 目录结构 main,入口层 from flask import Flaskfrom flask_sqlalchemy import SQLAlchemy app = ...
- 在性能测试时使用nmon进行监控服务器性能
在使用Jmeter进行性能测试,可以使用nmon进行服务器的监控. 一.nmon说明 nmon分为工具包和分析包(nmonanalyser) nmon安装很简单,根据服务器版本,下载相应的版本后,进行 ...
- 远程连接Mysql报错 java.sql.SQLException:null,message from server ... is not allowed to connect
在MySQL命令行输入如下命令: use mysql; select host from user; update user set host ='%' where user ='root'; 然后重 ...
- myBatis 基于javaBean配置
MyBatis的持久化解决方案是将用户从原始的JDBC访问中解放出来,用户只需要定义需要操作的SQL语句, 无须关注底层的JDBC操作,就可以以面向对象的方式来进行持久化层操作.底层数据库连接的获取, ...
- C语言I-博客作业04
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 C语言I博客作业04 我在这个课程的目标是 掌握使用for循环语句实现指定次数的循环程序设计 这个作业在那个具体方面帮助我实现目标 在编写 ...
- pg_receivewal实践
测试从pg_receivewal的日志中恢复从库为主库: 主从配置async模式,配置pg_receivewal接收日志pg_receivewal -D /dbaas/pg/data/pg_recei ...
- sprintf()函数可能发生的错误
接收到如下数据: GET http://app.tdvpn.com/heartbeat?mac=898607B81017AT+CIPSTATUS? &status=/ HTTP/1.1 Hos ...
- MySQL-快速入门(11)用户管理
1.权限表 存储用户权限信息表主要有:user.db.host.tables_priv.columns_priv.procs_priv. 1>user表: 记录允许连接到服务器的账号信息,里面的 ...
- HashMap底层为什么一定用数组
HashMap源码数据结构: Entry[] table = new Entry[capacity]; 其中,Entry就是一个链表节点.如果将数组替换成LinkedList是否可行?如下: List ...