【分治】【二分】POJ 3714 Raid
来源:互联网 发布:java分布式开发技术 编辑:程序博客网 时间:2024/05/20 19:15
题目
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
题目大意
给出平面上的N个发电厂和N个特工的坐标,求出离某个发电厂最近的特工与发电厂的距离。
思路
这里涉及到一个重要知识点:求平面上最近点对
知道了这个,就很容易做了(不是一模一样吗……),每个点多一个属性:阵营(flag),电厂的flag为0,特工为1,计算dis时,如果两个点的flag相同返回极大值即可。
代码
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;#define MAXN 100000#define INF 0x7fffffffstruct point{ double x,y; int f;//点的“阵营”}a[MAXN*2+5];//注意是两倍int N;int t[MAXN*2+5];/*以下是“求平面最小点对”的模板*/bool cmp1(point x,point y){return x.x<y.x;}bool cmp2(int x,int y){return a[x].y<a[y].y;}double dis(point x,point y){ if(x.f==y.f) return INF;//f一样则返回极大值 return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));}double F(int l,int r){ if(r-l==0) return INF; if(r-l==1) return dis(a[l],a[r]); int mid=(l+r)>>1; double ans=min(F(l,mid),F(mid+1,r)); int cnt=0; for(int i=l;i<=r;i++) if(a[i].x>=a[mid].x-ans&&a[i].x<=a[mid].x+ans) t[++cnt]=i; sort(t+1,t+cnt+1,cmp2); for(int i=1;i<=cnt;i++) for(int j=i+1;j<=cnt;j++) { if(a[t[j]].y>=a[t[i]].y+ans) break; ans=min(ans,dis(a[t[i]],a[t[j]])); } return ans;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&N); for(int i=1;i<=N;i++) { scanf("%lf%lf",&a[i].x,&a[i].y); a[i].f=0;//电厂 } for(int i=1;i<=N;i++) { scanf("%lf%lf",&a[N+i].x,&a[N+i].y); a[N+i].f=1;//特工 } N*=2; sort(a+1,a+N+1,cmp1); printf("%.3lf\n",F(1,N)); }}
- 【分治】【二分】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 1422 Air Raid (二分匹配)
- POJ 1422 - Air Raid(二分图匹配)
- 【二分图】 poj 1422 Air Raid
- poj 3714 Raid
- POJ 3714 raid
- POJ 3714 Raid
- POJ 3714 Raid
- poj 3714 Raid
- POJ 3714 Raid
- POJ 3714 Raid
- java8新增的日期,时间包以及格式器
- python 基础-1
- JAVA的学习 一
- MySQL主外键讲解
- studio设置JDK或者D就会无效
- 【分治】【二分】POJ 3714 Raid
- 安装MySQL最后一步出现错误Error Nr.1045
- 水题 第六站 HDU Safecracker
- python一道关于异常处理的问题
- interlliJ idea 与 sonarQube 的集成使用
- Ubuntu16.04安装R及RStudio
- 无法言表<SET集合的使用>
- 单选框回显
- 正则表达式之小括号