zoukankan      html  css  js  c++  java
  • poj2187 求平面最远点对,garham_scan算法求凸包

    poj2187 求平面最远点对,garham_scan算法求凸包

    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 29666   Accepted: 9180

    Description

    Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates. 

    Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms. 

    Input

    * Line 1: A single integer, N 

    * Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm 

    Output

    * Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

    Sample Input

    4
    0 0
    0 1
    1 1
    1 0
    

    Sample Output

    2
    

    Hint

    Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 
     
    题意:求平面最远点对
    思路:先求凸包,在凸多边形的点中暴力解出答案,槽点:用double超时,int才过。。
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<math.h>
    #include<ctype.h>
    
    using namespace std;
    
    const int maxn=1000100;
    const int INF=(1<<28);
    const int EPS=0.00000001;
    
    int N;
    struct Point
    {
        int x,y;
        friend int operator*(Point A,Point B)
        {
            return A.x*B.y-A.y*B.x;
        }
        friend Point operator-(Point A,Point B)
        {
            return {A.x-B.x,A.y-B.y};
        }
    };
    Point farm[maxn];
    Point ans_farm[maxn];
    int ans_top;
    
    int dist2(Point A,Point B)
    {
        int tx=A.x-B.x,ty=A.y-B.y;
        return tx*tx+ty*ty;
    }
    
    bool cmpYX(const Point A,const Point B)
    {
        if(A.y<B.y) return true;
        if(A.y==B.y){
            return A.x<=B.x;
        }
        return false;
    }
    
    bool cmp(const Point A,const Point B)
    {
        int tmp=(A-farm[0])*(B-farm[0]);
        return tmp>0;
    }
    
    void graham_scan()
    {
        sort(farm,farm+N,cmpYX);
        sort(farm+1,farm+N,cmp);
        ans_top=0;
        ans_farm[ans_top++]=farm[0];
        ans_farm[ans_top++]=farm[1];
        for(int i=2;i<N;i++){
            if((ans_farm[ans_top-1]-ans_farm[ans_top-2])*(farm[i]-ans_farm[ans_top-1])<0){
                ans_top--;
                ans_farm[ans_top++]=farm[i];
            }
            else ans_farm[ans_top++]=farm[i];
        }
    }
    
    int main()
    {
        while(cin>>N){
            for(int i=0;i<N;i++){
                scanf("%d%d",&(farm[i].x),&(farm[i].y));
            }
            graham_scan();
            int ans=0;
            for(int i=0;i<ans_top-1;i++){
                for(int j=i+1;j<ans_top;j++){
                    int tmp=dist2(ans_farm[i],ans_farm[j]);
                    if(tmp>ans) ans=tmp;
                }
            }
            printf("%d
    ",(int)ans);
        }
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    react项目建立导入包问题总结
    React中创建组件的3种方式
    export default与export的区别
    vue中的过滤器
    git clone https://chromium.googlesource.com/失败
    typescript 实现函数重载
    Rename a local and remote branch in git
    使用typescript开发react应用
    自己实现一个Promise库
    【翻译】Webpack 4 从0配置到生产模式
  • 原文地址:https://www.cnblogs.com/--560/p/4396997.html
Copyright © 2011-2022 走看看