poj 3714 最近点对 分治算法
最近点对_分治算法O(nlgn)
思路:对所有点先按x不减排序,
二分x,得到点集S1,点集S2,通过递归求得S1,S2的最小点对距离d1,d2;D=min{d1,d2};
合并S1,S2:找到在S1,S2划分线左右距离为D的所有点,按y不减(不增也可以)排序
循环每个点找它后面6个点的最小距离;
最后即求得最小点对距离。
若要求得点对坐标,在求值是保存点的坐标即可。
最近点对还有随机算法 时间复杂度O(n)。
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 3134 | Accepted: 1055 |
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
Source
#include <stdio.h> #include <algorithm> #include <math.h> #define INF (1<<30) using namespace std; long long x[200010],y[200010]; int orderx[200010],ordery[200010],n; double solve(int start,int end); bool cmpx(int i,int j) { if(x[i]<x[j])return 1; else return 0; } bool cmpy(int i,int j) { if(y[i]<y[j])return 1; else return 0; } double disx(int i,int j) { if(orderx[i]<n&&orderx[i]<n) return INF; else if(orderx[i]>=n&&orderx[i]>=n) return INF; else return sqrt((double)((x[orderx[i]]-x[orderx[j]])*(x[orderx[i]]-x[orderx[j]])+(y[orderx[i]]-y[orderx[j]])*(y[orderx[i]]-y[orderx[j]]))); } double disy(int i,int j) { if(ordery[i]<n&&ordery[j]<n) return INF; else if(ordery[i]>=n&&ordery[j]>=n) return INF; else return sqrt((double)((x[ordery[i]]-x[ordery[j]])*(x[ordery[i]]-x[ordery[j]])+(y[ordery[i]]-y[ordery[j]])*(y[ordery[i]]-y[ordery[j]]))); } double min(double a,double b){return a>b?b:a;} double min(double a,double b,double c) { return min(a,min(b,c)); } int main() { int T,i,j; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=0;i<2*n;i++) { scanf("%d%d",x+i,y+i); orderx[i]=ordery[i]=i; } sort(orderx,orderx+2*n,cmpx); printf("%.3lf\n",solve(0,2*n-1)); } return 0; } double solve(int start,int end) { if(start+1==end) return disx(start,end); else if(start+2==end) return min(disx(start,start+1),disx(start,start+2),disx(start+1,start+2)); else { int mid=start+(end-start)/2; double d=min(solve(start,mid),solve(mid+1,end)); int t=0; for(int i=start;i<=end;i++) if(fabs((double)x[orderx[i]]-(double)x[orderx[mid]])<=d) ordery[t++]=orderx[i]; sort(ordery,ordery+t,cmpy); for(int i=0;i<t;i++) for(int j=i+1;j<min(end,i+6);j++) d=min(d,disy(i,j)); return d; } }
posted on 2011-11-08 13:19 sleeper_qp 阅读(2611) 评论(0) 编辑 收藏 举报