Raid————分治与归并

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


平面的分治法,只不过这道题把点分成了两个集合,所以我们需要标记一下,在同一个集合里的元素,让它们计算出来的距离为INF
就好了


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int MAXN=200005;
const double INF=1e30;

struct node{
    double x,y;
    int flag;
}s[MAXN],re[MAXN]; 

bool cmpx(node a,node b)
{
    return a.x<b.x;
}
bool cmpy(node a,node b)
{
    return a.y<b.y;
}
double dis(node a,node b)
{
    if(a.flag == b.flag)    return INF;
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double slove(int l,int r)
{
    if(l == r)  return INF;
    int mid=(l+r)>>1;
    int cnt=0;
    double ans=min(slove(l,mid),slove(mid+1,r));
    for(int i=l;i<=r;i++)
        if(fabs(s[i].x-s[mid].x) <= ans)
            re[cnt++]=s[i];
    sort(re,re+cnt,cmpy);
    for(int i=0;i<cnt;i++)
        for(int j=i+1;j<cnt;j++)
            if(re[j].y-re[i].y>ans)
                break;
            else
                ans=min(ans,dis(re[i],re[j]));
    return ans;
}
int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        memset(re,0,sizeof(re));
        memset(s,0,sizeof(s));
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf %lf",&s[i].x,&s[i].y);
            s[i].flag=0;
        }
        for(int i=n;i<2*n;i++)
        {
            scanf("%lf %lf",&s[i].x,&s[i].y);
            s[i].flag=1;
        }           
        sort(s,s+2*n,cmpx);
        printf("%.3lf\n",slove(0,2*n-1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hpuer_Random/article/details/81394313