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

    计算几何/旋转卡壳


      学习旋转卡壳请戳这里~感觉讲的最好的就是这个了……

      其实就是找面积最大的三角形?。。。并且满足单调……

      嗯反正就是这样……

      这是一道模板题

      好像必须写成循环访问?我在原数组后面复制了一遍点,结果挂了……改成cur=cur%n+1就过了QAQ

    //其实是不是数组没开够所以复制的方法就爆了?

    UPD:(2015年5月13日 20:40:45)

      其实是我点保存在1~n里面,所以复制的时候不能写p[i+n-1]=p[i]; 而应该是p[i+n]=p[i];……QAQ我是傻逼

     1 Source Code
     2 Problem: 2187        User: sdfzyhy
     3 Memory: 1044K        Time: 32MS
     4 Language: G++        Result: Accepted
     5 
     6     Source Code
     7 
     8     //POJ 2187
     9     #include<cstdio>
    10     #include<cstring>
    11     #include<cstdlib>
    12     #include<iostream>
    13     #include<algorithm>
    14     #define rep(i,n) for(int i=0;i<n;++i)
    15     #define F(i,j,n) for(int i=j;i<=n;++i)
    16     #define D(i,j,n) for(int i=j;i>=n;--i)
    17     #define pb push_back
    18     using namespace std;
    19     typedef long long LL;
    20     inline int getint(){
    21         int r=1,v=0; char ch=getchar();
    22         for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-1;
    23         for(; isdigit(ch);ch=getchar()) v=v*10-'0'+ch;
    24         return r*v;
    25     }
    26     const int N=100010;
    27     /*******************template********************/
    28     struct Poi{
    29         int x,y;
    30         Poi(){}
    31         Poi(int x,int y):x(x),y(y){}
    32         void read(){x=getint();y=getint();}
    33     }p[N],ch[N];
    34     typedef Poi Vec;
    35     Vec operator - (const Poi &a,const Poi &b){return Vec(a.x-b.x,a.y-b.y);}
    36     bool operator < (const Poi &a,const Poi &b){return a.x<b.x || (a.x==b.x && a.y<b.y);}
    37     inline int Cross(const Poi &a,const Poi &b){return a.x*b.y-a.y*b.x;}
    38     inline int Dot(const Poi &a,const Poi &b){return a.x*b.x+a.y*b.y;}
    39     int n,m,ans;
    40 
    41     void graham(Poi *p,int n){
    42         int size=0;
    43         sort(p+1,p+n+1);
    44         ch[++m]=p[1];
    45         F(i,2,n){
    46             while(m>1 && Cross(ch[m]-ch[m-1],p[i]-ch[m-1])<=0) m--;
    47             ch[++m]=p[i];
    48         }
    49         int k=m;
    50         D(i,n-1,1){
    51             while(m>k && Cross(ch[m]-ch[m-1],p[i]-ch[m-1])<=0) m--;
    52             ch[++m]=p[i];
    53         }
    54         if (n>1) m--;
    55     }
    56 
    57     void rot(Poi *p,int n){
    58         ans=0;
    59     //    F(i,1,n-1) p[n+i-1]=p[i];
    60     //    n=n*2-1;
    61         int cur=2;
    62         F(i,1,n){
    63             Vec v = p[i]-p[i+1];
    64             while(Cross(p[i+1]-p[i],p[cur+1]-p[i]) > Cross(p[i+1]-p[i],p[cur]-p[i]))
    65                 cur=(cur%n)+1;
    66             ans=max(ans,Dot(p[cur]-p[i],p[cur]-p[i]));
    67             ans=max(ans,Dot(p[cur+1]-p[i+1],p[cur+1]-p[i+1]));
    68         }
    69     }
    70                 
    71     int main(){
    72     #ifndef ONLINE_JUDGE
    73         freopen("2187.in","r",stdin);
    74     //    freopen("2187.out","w",stdout);
    75     #endif
    76         n=getint();
    77         F(i,1,n) p[i].read();
    78         graham(p,n);
    79         rot(ch,m);
    80         printf("%d
    ",ans);
    81         return 0;
    82     }
    View Code
    Beauty Contest
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 29879   Accepted: 9260

    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

    [Submit]   [Go Back]   [Status]   [Discuss]

  • 相关阅读:
    js中return false,return,return true的用法及区别
    C#中关于页面缓存
    项目运行时出现webconfig="machineApplication......."之类的错误
    后台页面无法找到前台页面控件的ID
    .NET对文件的多种操作
    前台JS代码向后台传递参数
    多条件查询的Gridview分页显示
    C#中对数据做视图处理RowFilter
    JavaScript自触发时的参数传递
    Sys.WebForms.PageRequestManagerTimeoutException:服务器请求超时
  • 原文地址:https://www.cnblogs.com/Tunix/p/4500730.html
Copyright © 2011-2022 走看看