【题目描述】
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?

【输入】
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.

【输出】
For each test case output the minimum distance with precision of three decimal placed in a separate line.

【样例输入】
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

【样例输出】
1.414
0.000

题目链接:https://vjudge.net/problem/POJ-3714

平面最近点对问题

代码如下:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
#define int long long
static const int INF=0x3f3f3f3f3f3f3f3f;
static const int MAXN=1000000;
struct Node{int x,y,z;
}a[MAXN*2+10],b[MAXN*2+10];
int T,n;
bool cmp1(Node a,Node b) {if(a.x==b.x)return a.y<b.y;return a.x<b.x;
}
bool cmp2(Node a,Node b) {if(a.y==b.y)return a.x<b.x;return a.y<b.y;
}
double dist(Node p,Node q) {if(p.z==q.z) return INF;return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
}
double solve(int l,int r)
{int mid=(l+r)>>1;if(r==l)return INF;if(r-l==1)return dist(a[l],a[r]);double ans=min(solve(l,mid),solve(mid+1,r));int t=0;for(int i=l;i<=r;i++)if(a[mid].x-ans<=a[i].x && a[i].x<=a[mid].x+ans)b[++t]=a[i];sort(b+1,b+t+1,cmp2);for(int i=1;i<=t;i++)for(int j=i+1;j<=t;j++){if(b[j].y>=b[i].y+ans)break;ans=min(ans,dist(b[i],b[j]));}return ans;
}
signed main()
{std::ios::sync_with_stdio(false);std::cin.tie(0),cout.tie(0);cin>>T;while(T--){cin>>n;for(int i=1;i<=n;i++){cin>>a[i].x>>a[i].y;a[i].z=1;}for(int i=1;i<=n;i++){cin>>a[i+n].x>>a[i+n].y;a[i+n].z=2;}sort(a+1,a+n*2+1,cmp1);cout<<fixed<<setprecision(3)<<solve(1,2*n)<<endl;}return 0;
}

POJ - 3714 Raid【分治】【二分】相关推荐

  1. POJ - 3714 Raid(平面最近点对模板题,几何)

    题目链接:点击查看 题目大意:给出两个含有n个点的集合,在两个集合中分别任选一点,使得这两个点之间的距离最小 题目分析:因为n给到了1e5,所以n*n的暴力肯定是不行了,直接从网上copy了个分治优化 ...

  2. poj 3714 Raid

    大意:给你一些加油站A的坐标,一些需要基地B的坐标,问你这两者之间的最小值. 思路:用一个标记来表示分别在A和B中,数组长度变大了两倍,然后就是最近点对模板题啦. 另外:输出是%.3f,我用%.3lf ...

  3. 分治应用--最近点对问题 POJ 3714

    文章目录 1. 问题描述 2. 解题思路 3. 实现代码 4. POJ 3714 1. 问题描述 二维平面上有n个点,如何快速计算出两个距离最近的点对? 2. 解题思路 暴力做法是,每个点与其他点去计 ...

  4. Linux等比数列脚本求和,POJ 1845 (约数和+二分等比数列求和)

    题目大意:A^B的所有约数和,mod 9901. 解题思路: ①整数唯一分解定理: 一个整数A一定能被分成:A=(P1^K1)*(P2^K2)*(P3^K3).....*(Pn^Kn)的形式.其中Pn ...

  5. poj 1064 java_poj 1064(二分答案)

    题意: 有N条绳子,长度分别为 length[1,2,3,........,N]. 如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长有多长? 结果保留两位小数. 题解: 二分可能的长度. AC ...

  6. POJ - 1905 (几何+二分)

    POJ - 1905 (几何+二分) 题目正文如下: When a thin rod of length L is heated n degrees, it expands to a new leng ...

  7. LeetCode4_编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ““。(解决方案:横向扫描、 纵向扫描 、分治 二分查找 、秀儿操作之排序比较头尾)

    题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow&q ...

  8. POJ 1741 树分治

    题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...

  9. poj 3258 River Hopscotch 二分答案

    题目地址: http://poj.org/problem?id=3258 题目思路: 首先,如果只减少一部,那么一定要干掉最短的那段距离(一旦不消灭,最小的还是它,并没有达到使最小值取最大的理想情况) ...

最新文章

  1. 日期相减计算年_Excel教程:excel日期问题的小妙招
  2. 【C 语言】结构体 ( 结构体类型变量初始化 | 定义变量时进行初始化 | 定义隐式结构体时声明变量并初始化 | 定义普通结构体时声明变量并初始化 )
  3. 4种动态加载JS的方法
  4. 创建故障转移群集,LiveMigration系列之六
  5. 张旭升20162329 2006-2007-2 《Java程序设计》第一周学习总结
  6. mysql运算结果放入表中_MySQL表1新增数据,计算开始、结束日期之间所有时间,插入到表2中...
  7. 汇编和可执行文件(Debug和Release)
  8. ES6-weakset集合
  9. Oracle小复习(1)
  10. mongodb更新操作
  11. 无心剑随感《最完美的图形——圆》
  12. 对I2C总线的时钟同步和总线仲裁的深入理解
  13. SpringBoot + MyBatis + Thymeleaf 之 HelloWorld
  14. 树莓派进阶之路 (023) - Windows下用串行连接控制树莓派(转)
  15. 大型企业网络配置系列课程详解(七) --NAT的配置与相关概念的理解
  16. Mac终端查看MD5/SHA1/SHA256
  17. Mysql(多级分销)无限极数据库表设计方法
  18. ​网线水晶头排线图片接线标准
  19. can收发器 rx_CANOpen系列教程03_CAN收发器功能、原理及作用
  20. JSON Parse error:Unrecognized token xxx

热门文章

  1. 我的2011--快乐最重要
  2. List转数组的方法
  3. python中字典已经enumerate简单使用
  4. 激光SLAM论文阅读
  5. Android10双sim卡修改默认流量卡问题
  6. 第八届河南省程序设计大赛~~挑战密室 nyoj 1236
  7. 区块链公司谈区块链未来走向
  8. 如何在DAppNode中配置Ropsten节点?
  9. 2019 KDD accpeted paper2019年kdd接收的论文
  10. 项目中报错 Uncaught (in promise)