POJ 3714 Raid

来源:互联网 发布:淘宝手游充值是真的吗 编辑:程序博客网 时间:2024/05/16 11:45

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

方法一:分治
先按横坐标排序,然后分治,合并的时候只需要处理两边x坐标差在d范围内的点即可。把这些点找出来之后,再按照y值排序,然后再检查y坐标差在d以内的点,那么可以保证每个点最多检查6个点(否则这些点之间必定存在距离小于d的点对),那么可以保证o(n)的合并。总的复杂度nlognlogn。
代码如下

#include<algorithm>#include<iostream>#include<cfloat>#include<cstdio>#include<cmath>using namespace std;const double mx=DBL_MAX;struct pnt{    double x,y;    bool flg;}a[200001],tmp[200001];int n;inline double dist(const pnt &c, const pnt &d){    return sqrt(pow(c.x-d.x,2)+pow(c.y-d.y,2));}inline bool cmp(const pnt &c, const pnt &d){    if(c.x==d.x)        return c.y<d.y;    return c.x<d.x;}double mrg(int l,int r){    if(l==r)        return mx;    if(l+1==r)    {        if(a[l].flg!=a[r].flg)            return dist(a[l],a[r]);        else            return mx;    }    int mid=(l+r)/2,num=0;    double rlt=min(mrg(l,mid),mrg(mid+1,r));    for(int i=l;i<=r;++i)        if(dist(a[i],a[mid])<=rlt)            tmp[++num]=a[i];    sort(tmp+1,tmp+num+1,cmp);    for(int i=1;i<num;++i)        for(int j=i+1;j<=num;++j)            if(a[i].flg!=a[j].flg)            {                if(a[j].x-a[i].x>=rlt)                    break;                rlt=min(rlt,dist(a[i],a[j]));            }    return rlt;}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].flg=0;        }        for(int i=n+1;i<=2*n;++i)        {            scanf("%lf%lf",&a[i].x,&a[i].y);            a[i].flg=1;        }        sort(a+1,a+2*n+1,cmp);        printf("%.3f\n",mrg(1,2*n));    }    return 0;}

方法二:暴力枚举
注意47,48行,一定要剪枝。甚至可以比分治快。。。
奉上代码

#include<algorithm>#include<iostream>#include<cfloat>#include<cstdio>#include<cmath>using namespace std;struct pnt{    double x,y;    bool flg;}pt[200001];int n;double mn;inline double dist(const pnt &c,const pnt &d){    return sqrt((c.x-d.x)*(c.x-d.x)+(c.y-d.y)*(c.y-d.y));}inline bool cmp(const pnt &c, const pnt &d){    if(c.x==d.x)        return c.y<d.y;    return c.x<d.x;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        mn=DBL_MAX;        scanf("%d",&n);        for(int i=1;i<=n;++i)        {            scanf("%lf%lf",&pt[i].x,&pt[i].y);            pt[i].flg=0;        }        for(int i=n+1;i<=2*n;++i)        {            scanf("%lf%lf",&pt[i].x,&pt[i].y);            pt[i].flg=1;        }        sort(pt+1,pt+2*n+1,cmp);        for(int i=1;i<2*n;i++)            for(int j=i+1;j<=2*n;++j)                if(pt[i].flg!=pt[j].flg)                {                    if(pt[j].x-pt[i].x>=mn)                        break;                    mn=min(mn,dist(pt[i],pt[j]));                }        printf("%.3f\n",mn);    }    return 0;}

注意
输出格式,一定是”%.3f\n”,若是”%.3lf\n”就wa。(也不知道poj怎么想的)

0 0