poj3714 Raid(分治法…

Description

After successive failures in the battlesagainst the Union, the Empire retreated to its last stronghold.Depending on its powerful defense system, the Empire repelled thesix waves of Union's attack. After several sleepless nights ofthinking, Arthur, General of the Union, noticed that the onlyweakness of the defense system was its energy supply. The systemwas charged by N nuclear power stations andbreaking down any of them would disable the system.

Thegeneral soon started a raid to the stationsby N special agents who wereparadroped into the stronghold. Unfortunately they failed to landat the expected positions due to the attack by the Empire AirForce. As an experienced general, Arthur soon realized that heneeded to rearrange the plan. The first thing he wants to know nowis that which agent is the nearest to any power station. Could you,the chief officer, help the general to calculate the minimumdistance between an agent and a station?

Input

Thefirst line is a integer T representing the number oftest cases.
Each test case begins with aninteger N (1≤ N ≤ 100000).
Thenext N lines describe thepositions of the stations. Each line consists of twointegers X (0≤ X ≤ 1000000000)and Y (0≤ Y ≤ 1000000000) indicatingthe positions of the station.
The nextfollowing N lines describe thepositions of the agents. Each line consists of twointegers X (0≤ X ≤ 1000000000)and Y (0≤ Y ≤ 1000000000) indicatingthe positions of the agent.  

Output

Foreach test case output the minimum distance with precision of threedecimal 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

Source

 

解析:

       题目大意:给你两个集合A和B,求出最近点对,前提是两个点分别在不同的集合。典型的分治法求解最近点对。

 

代码:

 

#include

#include

#include

#include

using namespace std;

const int Max=200001;

const double inf=1e-8;

const double off=1e100;

struct Point{

    double x,y;

    bool flag;

};

Point p[Max];

inline int dbcmp(double tp){return tp<-inf?-1:tp>inf;}

inline double Distance(Point a,Point b)

{

        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

}

bool cmpx(Point a,Point b){return a.x

bool cmpy(int a,int b){return p[a].y

int ypos[Max];

double closedis(Point *p,int l,int r)

{

    if(l==r)    return off;

    if(l+1==r){

        if(p[l].flag==p[r].flag)    return off;

        else return Distance(p[l],p[r]);

    }

    int mid=(l+r)/2;

    double ans=min(closedis(p,l,mid),closedis(p,mid+1,r));                                       //不要使用__min函数,会超时

    int num=0;

    for(int i=l;i<=r;i++)                                                                                          //将与mid点横坐标差距不超过ans的点收集起来

    {

            if(dbcmp(fabs(p[i].x-p[mid].x)-ans)<=0)

                ypos[num++]=i;

    }

    sort(ypos,ypos+num,cmpy);

    for(int i=0;i

    {

        for(int j=i+1;j

        {

            if(dbcmp(fabs(p[ypos[i]].y-p[ypos[j]].y)-ans)>=0||

            else if(p[ypos[i]].flag!= p[ypos[j]].flag) 
               ans=min(ans,Distance(p[ypos[i]],p[ypos[j]]));

 

        }

    }

        return ans;

}

 

int main()

{

        int n,m;

        scanf("%d",&m);

        while(m--)

        {

            scanf("%d",&n);

            for(int i=0;i

                scanf("%lf%lf",&p[i].x,&p[i].y), p[i].flag=(ifalse:true);

           sort(p,p+n*2,cmpx);                                                          

           printf("%.3lf\n",closedis(p,0,n+n-1));

 

         }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值