zoukankan      html  css  js  c++  java
  • poj 2187:Beauty Contest(旋转卡壳)

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 32708   Accepted: 10156

    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) 
     
    题解:
      平面中给定N个点,让你求距离最远的点对。首先距离最远的一对点肯定在凸包上,所以可以先用Graham算法搞一遍凸包。
      如果枚举凸包的顶点,则效率太低。根据旋转卡壳,我们设两条平行线,当一条平行线与一条凸包上的边重合时,另一条平行线所过的点与离其最远的点一定是与凸包重合的那条平行线上的两点之一。由此性质,我们可以逆时针枚举凸包上的边,然后找离这条边距离最远的点,用这个点与线段两端点的距离更新答案。判断距离相等的条件是三点构成三角形的面积最大,这个可以用向量的叉积判断。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<cstring>
     7 #include<vector>
     8 #include<queue>
     9 using namespace std;
    10 const double eps=1e-8;
    11 int top,N;
    12 int ANS;
    13 struct P{
    14     int x,y;
    15     friend P operator-(P a,P b){
    16         P t; t.x=a.x-b.x; t.y=a.y-b.y;
    17         return t;
    18     }
    19     friend double operator*(P a,P b){
    20         return a.x*b.y-b.x*a.y;
    21     }
    22 }p[50005],s[50005];
    23 
    24 inline int dis(P a,P b){
    25     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    26 }
    27 inline bool operator<(P a,P b){
    28     int t=(a-p[1])*(b-p[1]);
    29     if(abs(t)<=eps) return dis(a,p[1])<dis(b,p[1]);
    30     return t>0;
    31 }
    32 inline void graham(){
    33     int tmp=1;
    34     for(int i=2;i<=N;i++){
    35         if(p[i].y<p[tmp].y||(p[i].y==p[tmp].y&&p[i].x<p[tmp].x)) tmp=i;
    36     }
    37     swap(p[1],p[tmp]);
    38     sort(p+2,p+N+1);
    39     s[1]=p[1]; s[2]=p[2]; top=2;
    40     for(int i=3;i<=N;i++){
    41         while(top>1&&(p[i]-s[top-1])*(s[top]-s[top-1])>=0) top--;
    42         s[++top]=p[i];
    43     }
    44 }
    45 inline int RC(){
    46     int q=2; ANS=0;
    47     s[top+1]=p[1];
    48     for(int i=1;i<=top;i++){
    49         while((s[i+1]-s[i])*(s[q+1]-s[i])>(s[i+1]-s[i])*(s[q]-s[i])) q=q%top+1;
    50         ANS=max(ANS,max(dis(s[q],s[i+1]),dis(s[q],s[i])));
    51     }
    52     return ANS;
    53 }
    54 int main(){
    55     scanf("%d",&N);
    56     for(int i=1;i<=N;i++){
    57         scanf("%d%d",&p[i].x,&p[i].y);
    58     }
    59     graham();
    60     printf("%d",RC());
    61     return 0;
    62 }
  • 相关阅读:
    HNU 12906 Battleship
    codeforces 261 D
    HDU 4939 Stupid Tower Defense(dp)
    HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007
    HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
    【转载】使用Pandas对数据进行筛选和排序
    【转载】使用pandas进行数据清洗
    【转载】VC维的来龙去脉
    Python-时间操作
    Pandas-数据导入
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/5249416.html
Copyright © 2011-2022 走看看