zoukankan      html  css  js  c++  java
  • Beauty Contest (凸包+旋转卡壳模板题)

    Beauty Contest

    题意:求凸包上最大点对的距离

    AC_Code:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <string>
     5 #include <algorithm>
     6 #include <cstring>
     7 using namespace std;
     8 typedef long long ll;
     9 const int maxn = 5e4+10;
    10 const double eps = 1e-8;
    11 
    12 
    13 typedef struct Point{
    14     double x,y;
    15     Point(double x=0,double y=0):x(x),y(y){}
    16 }Vector;
    17 
    18 Vector operator + (const Vector &A, const Vector &B){ return Vector(A.x+B.x,A.y+B.y); }
    19 Vector operator - (const Vector &A, const Vector &B){ return Vector(A.x-B.x,A.y-B.y); }
    20 Vector operator * (const Vector &A, double p){ return Vector(A.x*p,A.y*p); }
    21 Vector operator / (const Vector &A, double p){ return Vector(A.x/p,A.y/p); }
    22 
    23 bool operator <(const Point &A,const Point &B){
    24     return A.x<B.x || (A.x==B.x && A.y<B.y);
    25 }
    26 
    27 int dcmp(double x){
    28     if( fabs(x)<eps ) return 0;
    29     return x<0 ? -1 : 1;
    30 }
    31 
    32 double Cross(const Vector &A, const Vector &B){
    33     return A.x*B.y-A.y*B.x;
    34 }
    35 
    36 double Dot(const Vector &A, const Vector &B){
    37     return A.x*B.x+A.y*B.y;
    38 }
    39 
    40 double Length(const Vector &A){
    41     return Dot(A,A);
    42 }
    43 
    44 double Area2(const Point &A, const Point &B, const Point &C){
    45     return Cross(B-A,C-A);
    46 }
    47 
    48 int ConvexHull(Point *p,int n,Point *ch){
    49     sort(p,p+n);
    50     int m=0;
    51     for(int i=0;i<n;i++){
    52         while( m>1 && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0 ) m--;
    53         ch[m++]=p[i];
    54     }
    55     int k=m;
    56     for(int i=n-2;i>=0;i--){
    57         while( m>k && dcmp(Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<=0) m--;
    58         ch[m++]=p[i];
    59     }
    60     if( n>1 ) m--;
    61     return m;
    62 }
    63 
    64 Point p[maxn],ans[maxn];
    65 
    66 double RotatingCaliper(int m, Point *ch){
    67     int q=1;
    68     ch[m] = ch[0];
    69     double ans = 0.0;
    70     for(int p=0;p<m;p++){
    71         while( fabs( Area2(ch[p],ch[p+1],ch[q+1]) ) > fabs( Area2(ch[p],ch[p+1],ch[q]) ) ) q=(q+1)%m;
    72         ans = max(ans, max(Length(ch[q+1]-ch[p]), Length(ch[q+1]-ch[p+1])));
    73         ans = max(ans, max(Length(ch[q]-ch[p]), Length(ch[q]-ch[p+1])));
    74     }
    75     return ans;
    76 }
    77 
    78 int main()
    79 {
    80     int n;
    81     while( ~scanf("%d",&n) ){
    82         for(int i=0;i<n;i++){
    83             int x,y; scanf("%d%d",&x,&y);
    84             p[i] = Point(x,y);
    85         }
    86         int cnt = ConvexHull(p,n,ans);
    87         double res = RotatingCaliper(cnt,ans);
    88         printf("%.0f
    ",res);
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    ORACLE时间字段取年、月、日、季度【转】
    Oracle查询指定索引提高查询效率【转】
    ORACLE常用命令【转】
    ORACLE中LOB字段的使用和维护
    Oracle建立DBLINK的详细步骤记录【转】
    Oracle实用日期函数总结[转]
    js脚本中过滤特殊字符的正则表达式
    获取上一页面的URL的方法
    Repeat控件绑定数据格式显示
    我刚做的一个TreeView的CheckBox进行选中插入数据库,从数据库中读取数据后让CheckBox勾选的代码!
  • 原文地址:https://www.cnblogs.com/wsy107316/p/13528346.html
Copyright © 2011-2022 走看看