欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Raid POJ - 3714 (平面最近点对,分治)

程序员文章站 2022-03-30 08:51:38
...

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

大致题意:就是一个平面上有2*n个点,两个集合,每个集合有n个点,问两个不同集合的点之间的最短距离

思路:是hdu1007的变种,多加个判断即可

代码如下

#include <stdio.h>  
#include <math.h>  
#include <algorithm>  
using namespace std;  
// 不光有点,还要有集合类别标志  
struct node  
{  
    double x,y;  
    int f;  
}p[1000001];  
int arr[1000001];  
double Min(double a,double b)  
{  
    return a<b?a:b;  
}  
// 求两点之间的距离  
double dis(node a,node b)  
{  
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));  
}  
// 根据点横坐标or纵坐标排序  
bool cmp_y( int a,int b)  
{  
    return p[a].y<p[b].y;  
}  
bool cmp_x( node a,node b)  
{  
    return a.x<b.x;  
}  
// 求最近点对  
double close_pair( int l,int r )  
{  
    // 如果只剩下两个点,判断是否属于同一集合  
    if( r==l+1 )  
    {  
        if( p[l].f!=p[r].f )  
            return dis( p[l],p[r] );  
        else    return 1e9;  
    }  
    // 剩下三个点  
    else if( r==l+2 )  
    {  
        double ans=1e9;
        if(p[l].f!=p[l+1].f)
        ans=Min(ans,dis(p[l],p[l+1]));

        if(p[l+1].f!=p[r].f)
        ans=Min(ans,dis(p[l+1],p[r]));

        if(p[l].f!=p[r].f)
        ans=Min(ans,dis(p[l],p[r]));

        return ans;
    }  


    int mid=(l+r)/2;  
    double ans=Min(close_pair(l,mid),close_pair(mid+1,r));  

    int i,j,cnt=0;  
    for(i=l; i<=r; ++i)  
        if( p[i].x>=p[mid].x-ans && p[i].x<=p[mid].x+ans )  
            arr[cnt++]=i;  

    sort(arr,arr+cnt,cmp_y);  
    for( i=0; i<cnt ; i++ )  
        for(j=i+1; j<cnt; j++)  
        {  
            if( p[arr[j]].f != p[arr[i]].f )  
            {  
                if(p[arr[j]].y-p[arr[i]].y>=ans) break;  
                ans=Min(ans,dis(p[arr[i]],p[arr[j]]));  
            }  
        }  

    return ans;  
}  

int main()  
{  
    int n,t;  
    scanf("%d",&t);  
    while( t-- )  
    {  
        scanf("%d",&n);  
        for(int i=0;i<n;++i)  
        {  
            scanf("%lf%lf",&p[i].x,&p[i].y);  
            p[i].f=0;  
        }  
        for(int i=n;i<2*n;++i)  
        {  
            scanf("%lf%lf",&p[i].x,&p[i].y);  
            p[i].f=1;  
        }  
        sort(p,p+n+n,cmp_x);  
        printf("%.3lf\n",close_pair(0,2*n-1));  
    }  
    return 0;  
}