zoukankan      html  css  js  c++  java
  • POJ2187 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) 

    Source

     
     
    正解:凸包
    解题报告:
       2016年北京大学信息学奥赛训练营上机考核第二场 A题
           大致题意是求最远点对
       碰到这样一道水题,我居然没有AC,在考场上真是农飞了。话说pku尽考原题
           其实标准解法是先求凸包,再旋转卡壳。然而数据水,求完凸包,然后直接凸包上面跑暴力就可以了。
           考场上面没有调出来凸包,真是gi 
     
     
     1 //It is made by jump~
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<cstdlib>
     7 #include<algorithm>
     8 #include<map>
     9 #include<set>
    10 using namespace std;
    11 typedef long long LL;
    12 const int MAXN = 100011; 
    13 int n,m;
    14 LL ans;
    15 
    16 struct node{
    17     int x,y;
    18     node(int x=0,int y=0):x(x),y(y) {  }
    19     bool operator < (const node a)const{
    20     if(a.x==x) return a.y>y;
    21     return a.x>x;
    22     }
    23     node operator - (const node& o){
    24     return node(x-o.x, y-o.y);
    25     }
    26 }jump[MAXN],ch[MAXN*2];
    27 
    28 int getint()
    29 {
    30        int w=0,q=0;
    31        char c=getchar();
    32        while((c<'0' || c>'9') && c!='-') c=getchar();
    33        if (c=='-')  q=1, c=getchar();
    34        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
    35        return q ? -w : w;
    36 }
    37 
    38 int cross(node a,node b){
    39     return a.x*b.y-a.y*b.x;
    40 }
    41 
    42 LL dis(node a,node b){
    43     return (LL) ( (LL)(a.x-b.x)*(LL)(a.x-b.x) + (LL)(a.y-b.y)*(LL)(a.y-b.y)  );
    44 }
    45 
    46 int main()
    47 {
    48   n=getint();
    49   //for(int i=1;i<=n;i++) jump[i].x=getint(),jump[i].y=getint();
    50   for(int i=1;i<=n;i++)
    51       {
    52       int x=getint(),y=getint();
    53       jump[i]=node(x,y);
    54       } 
    55   sort(jump+1,jump+n+1);
    56 
    57   m=0;
    58   for(int i=1;i<=n;i++)
    59       {
    60       while(m>1 && cross(ch[m-1]-ch[m-2],jump[i]-ch[m-2])<=0) m--;
    61       ch[m++]=jump[i];
    62       }
    63   int k=m;
    64   for(int i=n-1;i>=1;i--)
    65       {
    66       while(m>k && cross(ch[m-1]-ch[m-2],jump[i]-ch[m-2])<=0) m--;
    67       ch[m++]=jump[i];
    68       }
    69 
    70   if(n>1) m--;
    71 
    72   ans=0;
    73   for(int i=1;i<=m;i++)
    74       for(int j=i+1;j<=m;j++){
    75       LL now=dis(ch[i],ch[j]);
    76       if(now>ans) ans=now;
    77       }
    78   printf("%lld",ans);
    79   return 0;
    80 }
  • 相关阅读:
    快速排序
    jenkins 升级
    JAVA中的Random()函数
    拦截器
    两个链表合并不加入新的链表空间
    统计字符 比如aaabbcca----3a2b1c1a
    折半查找两种实现
    字符串偏移
    java值传递
    基于zookeeper实现配置集中管理【转】
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5562431.html
Copyright © 2011-2022 走看看