zoukankan      html  css  js  c++  java
  • poj 2187 Beauty Contest

       http://poj.org/problem?id=2187  
    平面上有N个牧场,牧场位置互不相同,计算距离最远的俩个牧场间的距离,输出最远的平方。
    解:求n个点组成的凸包,凸包上找2个点一点是最远的。   
          Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 29979   Accepted: 9298

    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) 

    Source

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    
    struct node{
        double x,y;
        node(){};
        node(double _x, double _y)
         {
           x = _x;
            y = _y;
        }
    
        node operator - (const node & B) const
        {
            return node(x-B.x, y-B.y);
        }
    }p[50000], ch[50000];
    bool cmp(node a,node b)
    {
        if(a.x==b.x)
               return a.y<b.y;
        return a.x<b.x;
    }
    int squarDist(node A, node B) /**距离的平方*/
    {
        return (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y);
    }
    double Cross(node a,node b)//叉积。
    {
        return a.x*b.y-a.y*b.x;
    }
    int main()
    {
        int n,i,j;
        scanf("%d",&n);
        for(i=0;i<n;i++)
         scanf("%lf%lf",&p[i].x,&p[i].y);
          /** 求凸包 */
          /**先按照 x 从小到大排序, 再按照 y 从小到大排序*/
        sort(p,p+n,cmp);
         int m=0;
        for(i=0;i<n;i++)
        {
            while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)/** 从前往后找"下凸包" */
                m--;
              ch[m++]=p[i];
        }
        int k=m;
        for(i=n-2;i>=0;i--)
        {
             while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)/**从后往前找"上凸包", 形成完整的封闭背包*/
                 m--;
            ch[m++]=p[i];
    
        }
        if(n>1) /** 起点重复*/
           m--;
           int res=0;
      for(i=0;i<m;i++)
        for(j=i+1;j<m;j++)
         res=max(res, squarDist(ch[i], ch[j]));
        printf("%d
    ",res);
    
    
    }
  • 相关阅读:
    System.ServiceModel.CommunicationException: 接收HTTP 响应时错误发生
    "智囊"王沪宁先后辅佐三任总书记 _中国经济网
    xx
    我告诉你哦,最好吃的海南鸡饭不在海南…
    服务密码重置_中国移动通信
    移动服务密码怎么查_服务密码忘记了怎么办_百度经验
    http://www.sohu.com/a/162795109_465329
    首页--易配菜-中国餐饮行业最大的综合解决方案提供商
    浙江方圆工程咨询有限公司
    MySQL中间件方案盘点_搜狐科技_搜狐网
  • 原文地址:https://www.cnblogs.com/cancangood/p/4533804.html
Copyright © 2011-2022 走看看