首页 > 其他 > 详细

Raid

时间:2019-02-11 16:42:21      阅读:170      评论:0      收藏:0      [点我收藏+]

问题 a: Raid

时间限制: 1 Sec  内存限制: 128 MB
提交: 33  解决: 8
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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? 

 

输入

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. 

 

输出

For each test case output the minimum distance with precision of three decimal placed in a separate line.

 

样例输入

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

样例输出

1.414 
0.000
思路:stations和agents分别加标记,同一类标记为无穷大,然后求最近点对即可。
#include<bits/stdc++.h>
#include<vector>
using namespace std;
const int maxn=1e5+5;
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
#define inf 1e50
template <class T>
inline void rd(T &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < 0 || c > 9);
    while (c >= 0 && c <= 9){
        ret = ret * 10 + (c - 0), c = getchar();
    }
}
struct node{
    double x,y;
    int pos;
}p[2*maxn],tmp[2*maxn];
bool cx(node a,node b){return a.x<b.x;}
bool cy(node a,node b){return a.y<b.y;}
double gt(int l,int r){
    if(l==r)return inf;
    int mid=(l+r)/2;
    double ans=min(gt(l,mid),gt(mid+1,r));
    int cnt=0;
    for(int i=l;i<=r;i++){
        if(fabs(p[i].x-p[mid].x)<=ans){
            tmp[++cnt]=p[i];
        }
    }
    sort(tmp+1,tmp+1+cnt,cy);
    for(int i=1;i<=cnt;i++){
        for(int j=i+1;j<=cnt&&tmp[j].y-tmp[i].y<=ans;j++){
            double t;
            ans=min(ans,t=(tmp[j].pos==tmp[i].pos?inf:sqrt((tmp[i].x-tmp[j].x)*(tmp[i].x-tmp[j].x)+(tmp[i].y-tmp[j].y)*(tmp[i].y-tmp[j].y))));
        }
    }
    return ans;
}
int main(){
    int T;
    rd(T);
    while(T--){
        int n;
        rd(n);
        memset(tmp,0,sizeof(tmp));
        memset(p,0,sizeof(p));
        for(int i=1;i<=2*n;i++){
            scanf("%lf %lf",&p[i].x,&p[i].y);
            p[i].pos=i<=n?1:0;
        }
        sort(p+1,p+2*n+1,cx);
        printf("%.3f\n",gt(1,2*n));
    }
}

 

 

Raid

原文:https://www.cnblogs.com/czy-power/p/10362245.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!