zoukankan      html  css  js  c++  java
  • [计算几何][凸包直径]Beauty Contest

    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) 
     
    思路:1.先用Graham求解凸包 2.用Rotating_Caliper求凸包直径
    AC代码:
    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    int n;
    int top;
    int ans;
    struct Point{
      int x,y;
    }point[500100],stk[500100];
    
    int dis(Point a,Point b){
      return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    
    int cross(Point a,Point b,Point c){
      return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    }
    
    void find_miny(){
       Point tmp=point[0];
       int flag=0;
       for(int i=1;i<n;i++){
         if(point[i].y<tmp.y||(point[i].y==tmp.y&&point[i].x<tmp.x)){
            tmp=point[i];
            flag=i;
         }
       }
       if(flag) swap(point[0],point[flag]);
    }
    
    bool cmp(Point a,Point b){
      int n=cross(point[0],a,b);
      if(n>0||(n==0&&dis(point[0],a)>dis(point[0],b))) return true;//当point[0],a,b共线时,离point[0]远的排在前面
      return false;
    }
    
    void Graham(){
      top=-1;
      stk[++top]=point[0]; stk[++top]=point[1];
      for(int i=2;i<n;i++){
        while(top&&cross(stk[top-1],stk[top],point[i])<0) top--;
        stk[++top]=point[i];
      }
    }
    
    void RC(){//旋转卡(qia)壳
       ans=0.0;
       stk[++top]=point[0];
       int j=0;
       for(int i=1;i<=top;i++){
         while(abs(cross(stk[i-1],stk[i],stk[j]))<abs(cross(stk[i-1],stk[i],stk[j+1]))) j=(j+1)%n;
         ans=max(ans,max(dis(stk[j],stk[i-1]),dis(stk[j],stk[i])));
       }
    }
    
    int main()
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&point[i].x,&point[i].y);
        }
        if(n==2) {
            printf("%d
    ",dis(point[0],point[1]));
            return 0;
        }
        find_miny();
        sort(point+1,point+n,cmp);
        Graham();
        RC();
        printf("%d
    ",ans);
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    目录创建用什么命令?创建文件用什么命令?复制文件用什 么命令?
    哪些浏览器支持HTML 5?
    终端是哪个文件夹下的哪个文件?黑洞文件是哪个文件夹下 的哪个命令?
    什么是端到端微服务测试?
    HTML 5中的本地存储概念?
    HTML 5的页面结构和HTML 4或早先的HTML有什么不同?
    Spring 切面可以应用五种类型的通知?
    Math.round(11.5) 等于多少?Math.round(-11.5)等于多少?
    微服务架构如何运作?
    双因素身份验证的凭据类型有哪些?
  • 原文地址:https://www.cnblogs.com/lllxq/p/8991790.html
Copyright © 2011-2022 走看看