zoukankan      html  css  js  c++  java
  • POJ 2187 求凸包上最长距离

     简单的旋转卡壳题目

    以每一条边作为基础,找到那个最远的对踵点,计算所有对踵点的点对距离

    这里求的是距离的平方,所有过程都是int即可

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cmath>
     6 #include <algorithm>
     7 using namespace std;
     8 #define N 50010
     9 #define eps 1e-9
    10 int n , top;
    11 
    12 int dcmp(double x)
    13 {
    14     if(fabs(x)<eps) return 0;
    15     return x<0?-1:1;
    16 }
    17 
    18 struct Point{
    19     int x,y;
    20     Point(int x=0 , int y=0):x(x),y(y){}
    21     bool operator==(const Point &m) const{
    22         return x==m.x&&y==m.y;
    23     }
    24     int squaredDis(){return x*x+y*y;}
    25     void input(){scanf("%d%d" , &x , &y);}
    26     void print(){cout<<x<<" "<<y<<endl;}
    27 }po[N] , rec[N] , p;
    28 typedef Point Vector;
    29 
    30 Vector operator+(Vector a , Vector b){return Vector(a.x+b.x , a.y+b.y);}
    31 Vector operator-(Vector a , Vector b){return Vector(a.x-b.x , a.y-b.y);}
    32 Vector operator*(Vector a , double b){return Vector(a.x*b , a.y*b);}
    33 Vector operator/(Vector a , double b){return Vector(a.x/b , a.y/b);}
    34 
    35 int Cross(Vector a , Vector b){return a.x*b.y-b.x*a.y;}
    36 double Len(Vector a){return sqrt(a.x*a.x*1.0+a.y*a.y);}
    37 
    38 bool cmp(Point a , Point b){
    39     int v = Cross(a , b);
    40     if(v == 0) return a.x<b.x;
    41     else return v>0;
    42 }
    43 
    44 void Graham(Point *a , Point *rec)
    45 {
    46     sort(a , a+n , cmp);
    47   //  for(int i=0 ; i<n ; i++) cout<<i<<" "<<a[i].x<< " "<<a[i].y<<endl;
    48     rec[0] = a[0] , rec[1] = a[1];
    49     top=1;
    50     for(int i=2 ; i<n ; i++){
    51         while(top>0 && Cross(rec[top]-rec[top-1] , a[i]-rec[top-1])<=0)
    52             top--;
    53         rec[++top] = a[i];
    54     }
    55    // for(int i=0 ; i<=top ; i++) cout<<i<<" "<<rec[i].x<< " "<<rec[i].y<<endl;
    56     int tmp = top;
    57     for(int i=n-1 ; i>=0 ; i--){
    58         while(top>tmp && Cross(rec[top]-rec[top-1] , a[i]-rec[top-1])<=0)
    59             top--;
    60         rec[++top]=a[i];
    61     }
    62   //  for(int i=0 ; i<=top ; i++) cout<<i<<" "<<rec[i].x<< " "<<rec[i].y<<endl;
    63 }
    64 
    65 int maxDis(Point *a)
    66 {
    67     int la=top-1 , p=0 , q=0;
    68     int maxn = 0;
    69     for(p=0 ; p<top ; p++){
    70         while(Cross(a[p]-a[la] , a[q+1]-a[la]) - Cross(a[p]-a[la] , a[q]-a[la])>0)
    71             q=(q+1)%top;
    72         maxn = max(maxn , (a[q]-a[la]).squaredDis());
    73         maxn = max(maxn , (a[q]-a[p]).squaredDis());
    74         la = p;
    75     }
    76     return maxn;
    77 }
    78 
    79 int main()
    80 {
    81    // freopen("a.in" , "r" , stdin);
    82     while(~scanf("%d" , &n))
    83     {
    84         for(int i=0 ; i<n ; i++) po[i].input();
    85         p = po[0];
    86         for(int i=1 ;i<n ; i++)
    87             if(po[i].y<p.y||(po[i].y==p.y&&po[i].x<p.x)) p=po[i];
    88         Graham(po , rec);
    89         int ans = maxDis(rec);
    90         cout<<ans<<endl;
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    [置顶] 宏途_LCD调试流程.
    字典树的数据结构及基本算法的实现
    uva 10714 Ants(贪心)
    paip.输入法编程---增加码表类型
    chomp方法
    ios 限制输入长度
    我所理解的设计模式(C++实现)——策略模式(Strategy Pattern)
    Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解
    C#系列教程——switch定义及使用
    局域网内linux由ip反解析主机名
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/4622244.html
Copyright © 2011-2022 走看看