poj 3714

Raid
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 22430   Accepted: 5847

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

 思路: 这题还是可以用分治的思想来做,当然也可以用kd树来做。  

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<algorithm> 
 6 using namespace std;  
 7 int const N=100000+10;  
 8 double const inf=1e20; 
 9 struct node{
10     double x,y;  
11 }a[N<<1];  
12 int t[N<<1],n,c1[N],c2[N],cnt1,cnt2; 
13 int cmpx(int t1,int t2){
14     return a[t1].x<a[t2].x;  
15 }
16 int cmpy(int t1,int t2){
17     return a[t1].y>a[t2].y;  
18 }  
19 double sqr(double x){
20     return x*x;  
21 } 
22 double dist(int t1,int t2){
23     return sqrt( sqr(a[t1].x-a[t2].x)+sqr(a[t1].y-a[t2].y));  
24 } 
25 double calc(int l,int r,int mid,double d){
26     sort(c1+1,c1+cnt1+1,cmpy);  
27     sort(c2+1,c2+cnt2+1,cmpy); 
28     double ret=d;  
29     int k=1;  
30     for(int i=1;i<=cnt1;i++){
31         if(c1[i]>n) continue; 
32         while (k<=cnt2 && a[c2[k]].y-a[c1[i]].y>d) k++;  
33         for(int j=k;j<=cnt2;j++){
34             if(a[c2[j]].y-a[c1[i]].y>=-d) {
35                 if(c2[j]>n)
36                 ret=min(ret,dist(c1[i],c2[j])); 
37             }else break;  
38         }  
39     }
40     k=1;  
41     for(int i=1;i<=cnt1;i++){
42         if(c1[i]<=n) continue;  
43         while (k<=cnt2 && a[c2[k]].y-a[c1[i]].y>d) k++;  
44         for(int j=k;j<=cnt2;j++){
45             if(c2[j]<=n)
46                 ret=min(ret,dist(c1[i],c2[j]));
47         }
48     }   
49     return ret;  
50 }   
51 double solve(int l,int r){
52     if(l==r) return inf; 
53     if(l+1==r){
54         if(t[l]<=n && t[r]>n || t[l]>n && t[r]<=n)  
55             return dist(t[l],t[r]);  
56         else return inf; 
57     } 
58     int mid=(l+r)/2; 
59     double d1=solve(l,mid);  
60     double d2=solve(mid+1,r); 
61     double d=min(d1,d2);  
62     cnt1=cnt2=0;  
63     for(int i=l;i<=mid;i++){
64         double dd=a[t[mid]].x-a[t[i]].x;  
65         if(dd>=0 && dd<d) c1[++cnt1]=t[i];  
66     } 
67     for(int i=mid+1;i<=r;i++){
68         double dd=a[t[i]].x-a[t[mid]].x;  
69         if(dd>=0 && dd<d) c2[++cnt2]=t[i];  
70     } 
71     return calc(l,r,mid,d); 
72 } 
73 int main(){
74     int cas;  
75     scanf("%d",&cas);  
76     while(cas--){
77         scanf("%d",&n);  
78         for(int i=1;i<=2*n;i++)  
79             scanf("%lf%lf",&a[i].x,&a[i].y);  
80         for(int i=1;i<=2*n;i++) t[i]=i;  
81         sort(t+1,t+2*n+1,cmpx);  
82         printf("%0.3lf\n",solve(1,2*n)); 
83     } 
84     return 0; 
85 } 
View Code

猜你喜欢

转载自www.cnblogs.com/ZJXXCN/p/11117839.html