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

                                                Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 24738   Accepted: 7565

    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

     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <vector>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 struct point
    10 {
    11     int x,y;
    12     friend point operator - (const point &a,const point &b)
    13     {
    14         point t;
    15         t.x=a.x-b.x; t.y=a.y-b.y;
    16         return t;
    17     }
    18 };
    19 
    20 struct polygon_convex
    21 {
    22     vector<point> p;
    23     polygon_convex(int size=0)
    24     {
    25         p.resize(size);
    26     }
    27 };
    28 
    29 bool cmp(const point a,const point b)
    30 {
    31     return (a.x<b.x)||(a.x==b.x&&a.y<b.y);
    32 }
    33 
    34 int det(point a,point b)
    35 {
    36     return a.x*b.y-a.y*b.x;
    37 }
    38 
    39 int dist(point a,point b)
    40 {
    41     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    42 }
    43 
    44 polygon_convex convex_hull(vector<point> a)
    45 {
    46     polygon_convex res(a.size()*2+5);
    47     sort(a.begin(),a.end(),cmp);
    48     int m=0;
    49     for(int i=0;i<a.size();i++)
    50     {
    51         while(m>1&&det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2])<=0) m--;
    52         res.p[m++]=a[i];
    53     }
    54     int k=m;
    55     for(int i=int(a.size())-2;i>=0;i--)
    56     {
    57         while(m>k&&det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2])<=0) m--;
    58         res.p[m++]=a[i];
    59     }
    60     res.p.resize(m);
    61     if(a.size()>1) res.p.resize(m-1);
    62     return res;
    63 }
    64 
    65 int convex_diameter(polygon_convex &a)
    66 {
    67     vector<point> P=a.p;
    68     int n=P.size();
    69     int maxd=0;
    70     if(n==1) return maxd;
    71     #define next(i) ((i)+1)%n
    72     for(int i=0,j=1;i<n;i++)
    73     {
    74         while((det(P[next(i)]-P[i],P[j]-P[i])-det(P[next(i)]-P[i],P[next(j)]-P[i]))<0)
    75             j=next(j);
    76         int d=dist(P[i],P[j]);
    77         if(d>maxd) maxd=d;
    78         d=dist(P[next(i)],P[next(j)]);
    79         if(d>maxd) maxd=d;
    80     }
    81     return maxd;
    82 }
    83 
    84 int main()
    85 {
    86     int n;
    87     cin>>n;
    88     vector<point> a(n);
    89     polygon_convex bao;
    90     for(int i=0;i<n;i++)
    91         cin>>a[i].x>>a[i].y;
    92     bao=convex_hull(a);
    93     cout<<convex_diameter(bao)<<endl;
    94 /*
    95     for(int i=0;i<bao.p.size();i++)
    96         cout<<bao.p[i].x<<" , "<<bao.p[i].y<<endl;
    97 */
    98     return 0;
    99 }
  • 相关阅读:
    C# ConfigMan.cs
    C# 反序列化JSON
    C#求数组最大值或最大值位置索引
    C#获取DataGridView数据源DataSource类型
    C#动态添加DataGridView列和行
    C# WinForm 检测文件是否被占用
    C#开源组件NPOI处理Excel导入DataTable数据至Sheet
    C# WinForm 扩展指定列宽适应DataGridView显示行数(二)
    C# WinForm 扩展指定列宽适应DataGridView显示行数
    VFP9 对 SqlServer Image字段的图片存取处理
  • 原文地址:https://www.cnblogs.com/CKboss/p/3279119.html
Copyright © 2011-2022 走看看