【分治】【二分】POJ 3714 Raid

来源:互联网 发布:java分布式开发技术 编辑:程序博客网 时间:2024/05/20 19:15

题目

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

题目大意

给出平面上的N个发电厂和N个特工的坐标,求出离某个发电厂最近的特工与发电厂的距离。

思路

这里涉及到一个重要知识点:求平面上最近点对
知道了这个,就很容易做了(不是一模一样吗……),每个点多一个属性:阵营(flag),电厂的flag为0,特工为1,计算dis时,如果两个点的flag相同返回极大值即可。

代码

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;#define MAXN 100000#define INF 0x7fffffffstruct point{    double x,y;    int f;//点的“阵营”}a[MAXN*2+5];//注意是两倍int N;int t[MAXN*2+5];/*以下是“求平面最小点对”的模板*/bool cmp1(point x,point y){return x.x<y.x;}bool cmp2(int x,int y){return a[x].y<a[y].y;}double dis(point x,point y){    if(x.f==y.f) return INF;//f一样则返回极大值    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));}double F(int l,int r){    if(r-l==0)        return INF;    if(r-l==1)        return dis(a[l],a[r]);    int mid=(l+r)>>1;    double ans=min(F(l,mid),F(mid+1,r));    int cnt=0;    for(int i=l;i<=r;i++)        if(a[i].x>=a[mid].x-ans&&a[i].x<=a[mid].x+ans)            t[++cnt]=i;    sort(t+1,t+cnt+1,cmp2);    for(int i=1;i<=cnt;i++)        for(int j=i+1;j<=cnt;j++)        {            if(a[t[j]].y>=a[t[i]].y+ans) break;            ans=min(ans,dis(a[t[i]],a[t[j]]));        }    return ans;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&N);        for(int i=1;i<=N;i++)        {            scanf("%lf%lf",&a[i].x,&a[i].y);            a[i].f=0;//电厂        }        for(int i=1;i<=N;i++)        {            scanf("%lf%lf",&a[N+i].x,&a[N+i].y);            a[N+i].f=1;//特工        }        N*=2;        sort(a+1,a+N+1,cmp1);        printf("%.3lf\n",F(1,N));    }}
原创粉丝点击