zoukankan      html  css  js  c++  java
  • 【POJ3714】Raid:平面最近点对

    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个点的所有点对中,该点对的距离最小。
    但是在Raid这道题中,要求两个集合S1和S2中的最近点对,按理说代码应该更加复杂。
    然而,这里找到了一个更简单的做法。
    代码如下:
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int T,n;
    struct node{
        double x,y;
    }e[100055],d[100045];
    bool cmp(node a,node b)
    {
        if(a.x==b.x)return a.y<b.y;
        return a.x<b.x;
    }
    double dis(node a,node b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            double ans=1e11;
            scanf("%d",&n);
            for(register int i=1;i<=n;++i)
            {
                scanf("%lf%lf",&e[i].x,&e[i].y);
            }
            for(register int i=1;i<=n;++i)
            {
                scanf("%lf%lf",&d[i].x,&d[i].y);
            }
            sort(e+1,e+n+1,cmp);
            sort(d+1,d+n+1,cmp);
            int t = 1;
            for(int i=1;i<=n;++i)
            {
                while(t<n&&cmp(e[t+1],d[i])) ++t;//以d[i]的横坐标为分界线,将e点分为两部分 
                for(int j=t;j<=n;++j)//扫描{e}的第一部分 
                {
                    if(fabs(e[j].x-d[i].x)>ans) break;
                    ans= min(ans, dis(e[j],d[i]));
                    //往下扫描的同时,将ans向小更新。很显然,{e}经过排序之后,不需要几次就会break; 
                }
                for(int j=t-1;j;--j)//扫描{e}的另一部分 
                {
                    if(fabs(e[j].x-d[j].x)>ans) break;
                    ans = min(ans,dis(e[j],d[i]));//同上 
                }
            }
            printf("%.3f
    ",ans);
        }
        return 0;
    }
    
    
    
    
    
     
  • 相关阅读:
    SQL 2005的ROW_NUMBER()实现分页的功能
    UML建模工具集
    挽救崩溃的WinXP操作系统四招
    16个经典面试问题回答思路
    自动关闭弹出式窗口
    在winform中嵌入Flash(swf)的方法及常见问题的解决
    C#获取安装程序所在的路径
    转载:C#操作注册表
    C#实现Dll(OCX)控件自动注册的两种方法
    转载:C#创建和修改注册信息
  • 原文地址:https://www.cnblogs.com/719666a/p/10116037.html
Copyright © 2011-2022 走看看