• 【HDU 5721】Palace(平面最近点对)


    Palace

    Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 404    Accepted Submission(s): 104


    Problem Description
    The last trial Venus imposes on Psyche is a quest to the underworld. She is to take a box and obtain in it a dose of the beauty of Prosperina, queen of the underworld. 

    There are   palaces in the underworld, which can be located on a 2-Dimension plane with  (x,y coordinates (where  x, are integers). Psyche would like to find the distance of the closest pair of two palaces. It is the password to enter the main palace. 

    However, the underworld is mysterious and changes all the time. At different times, exactly one of the   palaces disappears.  

    Psyche wonders what the distance of the closest pair of two palaces is after some palace has disappeared. 

    Print the sum of the distance after every single palace has disappeared.  

    To avoid floating point error, define the distance   between palace  (x1,y1 and  (x2,y2 as  d=(x1x2)2+(y1y2).
     
    Input
    The first line of the input contains an integer    (1T5, which denotes the number of testcases. 

    For each testcase, the first line contains an integers    (3n105, which denotes the number of temples in this testcase. 

    The following   lines contains   pairs of integers, the  -th pair  (x,y  (105x,y105 denotes the position of the  -th palace.
     
    Output
    For each testcase, print an integer which denotes the sum of the distance after every single palace has disappeared.
     
    Sample Input
    1 3 0 0 1 1 2 2
     
    Sample Output
    12
    Hint
    If palace $ (0,0) $ disappears,$ d = (1-2) ^ 2 + (1 - 2) ^ 2 = 2 $; If palace $ (1,1) $ disappears,$ d = (0-2) ^ 2 + (0 - 2) ^ 2 = 8 $; If palace $ (2,2) $ disappears,$ d = (0-1) ^ 2 + (0-1) ^ 2 = 2 $; Thus the answer is $ 2 + 8 + 2 = 12 $。
     
    Source

    题目大意:给出平面上n个点的坐标,可以得到平面最近点对的距离。

    现求分别删掉第1~n个点时,平面上最近点对的距离的和。

    对于不删点时可以求得最近点对。

    如果删掉的点不是最近点对的两个端点,最近距离还是这两个点的距离。(会有n-2)次

    其次,如果删除其中任何一个点,标记一下,然后再分别跑一边最近点,统计即可。

    最近点对用分治法。

    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <stack>
    #include <list>
    #include <algorithm>
    #include <map>
    #include <set>
    #define LL long long
    #define Pr pair<int,int>
    #define fread(ch) freopen(ch,"r",stdin)
    #define fwrite(ch) freopen(ch,"w",stdout)
    
    using namespace std;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int msz = 100100;
    const int mod = 1e9+7;
    const double eps = 1e-8;
    
    struct Point
    {
        LL x,y;
        int id;
    };
    
    LL dist(Point a,Point b)
    {
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    
    Point p[msz];
    Point tmpt[msz];
    
    bool cmpxy(Point a,Point b)
    {
        if(a.x != b.x) return a.x < b.x;
        return a.y < b.y;
    }
    
    bool cmpy(Point a,Point b)
    {
        return a.y < b.y;
    }
    
    LL d;
    int pt[2];
    
    void Closest_Pair(int left,int right,int opt)
    {
        if(left == right) return;
        LL tmp;
        if(left + 1 == right)
        {
            if(p[left].id != opt && p[right].id != opt)
            {
                tmp = dist(p[left],p[right]);
                if(d > tmp)
                {
                    d = tmp;
                    if(opt == -1)
                    {
                        pt[0] = p[left].id;
                        pt[1] = p[right].id;
                    }
                }
            }
            return;
        }
    
        int mid = (left+right)/2;
        Closest_Pair(left,mid,opt);
        Closest_Pair(mid+1,right,opt);
    
        int k = 0;
    
        for(int i = left; i <= right; ++i)
        {
            if(p[i].id != opt && abs(p[mid].x - p[i].x) <= d)
                tmpt[k++] = p[i];
        }
    
        sort(tmpt,tmpt+k,cmpy);
    
        for(int i = 0; i < k; ++i)
        {
            for(int j = i+1; j < k && tmpt[j].y - tmpt[i].y < d; ++j)
            {
                tmp = dist(tmpt[i],tmpt[j]);
                if(d > tmp)
                {
                    if(opt == -1)
                    {
                        pt[0] = tmpt[i].id;
                        pt[1] = tmpt[j].id;
                    }
                    d = tmp;
                }
            }
        }
    }
    
    int main()
    {
        //fread("");
        //fwrite("");
    
        int t,n;
    
        scanf("%d",&t);
    
        while(t--)
        {
            scanf("%d",&n);
            for(int i = 0; i < n; ++i)
            {
                scanf("%lld%lld",&p[i].x,&p[i].y);
                p[i].id = i;
            }
    
            sort(p,p+n,cmpxy);
    
            d = INF;
            Closest_Pair(0,n-1,-1);
            LL ans = 1LL*d*(n-2);
    
            d = INF;
            Closest_Pair(0,n-1,pt[0]);
            ans += d;
    
            d = INF;
            Closest_Pair(0,n-1,pt[1]);
            ans += d;
            printf("%lld\n",ans);
        }
    
        return 0;
    }

    原文http://www.voidcn.com/article/p-bjxfjkwq-dr.html

  • 相关阅读:
    在 Eclipse Workbench 之外使用 Eclipse GUI
    GB2312,GBK,Unicode
    木偶一之推荐系统
    Matlab:任意矩阵计算分布密度(海明距离的分布密度)
    live555在arm linux下的交叉编译,并下载的Arm板播放H264文件
    java设计模式之原型模式
    HDU 1102
    poj3661另一种做法(滚动数组)
    基于QT的小游戏细菌病毒战
    某代码查看器的保护突破
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12673904.html
Copyright © 2020-2023  润新知