poj 3714(典型的最近点对问题)
2014-05-10 10:00
381 查看
Raid
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
Sample Output
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 8271 | Accepted: 2466 |
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
题目很容易理解就不多说了,上AC代码:
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; #define LL long long #define MM 1e18; struct Node{ LL x,y; int s; }a[200100],b[200100]; //注意这里数组大小,别忘了这里n的范围,结果runtime error了 int cmp1(Node t1,Node t2){ if(t1.x==t2.x) return t1.y<t2.y; return t1.x<t2.x; } int cmp2(Node t1,Node t2){ return t1.y<t2.y; } double dis(Node t1,Node t2){ if(t1.s==t2.s) return MM; //同一类型的点间距离设置为MM return (double)sqrt((t1.x-t2.x)*(t1.x-t2.x+0.0)+(t1.y-t2.y)*(t1.y-t2.y)); //这里别忘了+0.0,将结果在sqrt里转化为double型,否则会compile error } double closest(int left,int right){ //最近点对模板 if(left==right) return MM; if(left+1==right) return dis(a[left],a[right]); int mid=(left+right)/2; double d=min(closest(left,mid),closest(mid+1,right)); //左半边和右半边最小距离 //中间部分最小距离 int t=0; for(int i=left;i<=right;i++){ //离mid点横坐标差在d范围内的所有点 if(fabs(a[i].x-a[mid].x+0.0)<=d) b[t++]=a[i]; } sort(b,b+t,cmp2); //按y坐标排序 for(int i=0;i<t;i++) for(int j=i+1;j<t;j++){ if(a[j].y-a[i].y<=d) //只要判断y坐标差在d范围内的点即可 d=min(d,dis(a[i],a[j])); } return d; } int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; scanf("%d",&T); while(T--){ int n; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lld %lld",&a[i].x,&a[i].y); a[i].s=0; } for(int i=n+1;i<=2*n;i++){ scanf("%lld %lld",&a[i].x,&a[i].y); a[i].s=1; } sort(a+1,a+2*n+1,cmp1); printf("%.3lf\n",closest(1,2*n)); //注意输出格式 } return 0; }
相关文章推荐
- hdu 1010 Tempter of the Bone(dfs)
- AfxWinMain中pThread->InitInstance()出错的解决方法
- PID口诀
- HDU 4722-Good Numbers
- 新人报道!
- leetcode第一刷_Remove Duplicates from Sorted List II
- spring框架整合使用定时任务框架java quartz的示例代码配置
- 我是一个好人
- oracle 物化视图
- reverseNnumber 几种方法
- 富文本编辑器-ueditor基本配置
- OpenCV 环境搭建( Win7 32位 / VS2010 / OpenCV2.4.8 )
- hust 1013 Grid
- unity3d中布娃娃系统
- 定制iOS 7中的导航栏和状态栏
- 在smarty中使用if else
- C++STL_set中函数_集合运算
- 修改adb devices的设备ID/设备号
- 数据库架构的一些问题
- 以旧换新,iPhone5s免费拿