poj 3714(典型的最近点对问题)

Raid
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 8271 Accepted: 2466

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
题目很容易理解就不多说了,上AC代码:
 
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define MM 1e18;
struct Node{
    LL x,y;
    int s;
}a[200100],b[200100];    //注意这里数组大小,别忘了这里n的范围,结果runtime error了 
int cmp1(Node t1,Node t2){
    if(t1.x==t2.x)
        return t1.y<t2.y;
    return t1.x<t2.x;
}
int cmp2(Node t1,Node t2){
    return t1.y<t2.y;
}
double dis(Node t1,Node t2){
    if(t1.s==t2.s) return MM;   //同一类型的点间距离设置为MM 
    return (double)sqrt((t1.x-t2.x)*(t1.x-t2.x+0.0)+(t1.y-t2.y)*(t1.y-t2.y));   //这里别忘了+0.0,将结果在sqrt里转化为double型,否则会compile error 
}
double closest(int left,int right){   //最近点对模板 
    if(left==right) return MM;
    if(left+1==right) return dis(a[left],a[right]);
    int mid=(left+right)/2;
    double d=min(closest(left,mid),closest(mid+1,right));  //左半边和右半边最小距离 
    //中间部分最小距离 
    int t=0;
    for(int i=left;i<=right;i++){           //离mid点横坐标差在d范围内的所有点 
        if(fabs(a[i].x-a[mid].x+0.0)<=d)
            b[t++]=a[i];
    }
    sort(b,b+t,cmp2);        //按y坐标排序 
    for(int i=0;i<t;i++)     
        for(int j=i+1;j<t;j++){
            if(a[j].y-a[i].y<=d)           //只要判断y坐标差在d范围内的点即可 
                d=min(d,dis(a[i],a[j]));
        }
    return d;
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lld %lld",&a[i].x,&a[i].y);
            a[i].s=0;
        }
        for(int i=n+1;i<=2*n;i++){
            scanf("%lld %lld",&a[i].x,&a[i].y);
            a[i].s=1;
        }
        sort(a+1,a+2*n+1,cmp1);   
        printf("%.3lf\n",closest(1,2*n));  //注意输出格式 
    }
    return 0;
}


 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值