zoukankan      html  css  js  c++  java
  • P1452 Beauty Contes(旋转卡壳版)

    题目背景

    此处省略1W字^ ^

    题目描述

    贝茜在牛的选美比赛中赢得了冠军”牛世界小姐”。因此,贝西会参观N(2 < = N < = 50000)个农场来传播善意。世界将被表示成一个二维平面,每个农场位于一对整数坐标(x,y),各有一个值范围在-10000…10000。没有两个农场共享相同的一对坐标。

    尽管贝西沿直线前往下一个农场,但牧场之间的距离可能很大,所以她需要一个手提箱保证在每一段旅程中她有足够吃的食物。她想确定她可能需要旅行的最大可能距离,她要知道她必须带的手提箱的大小。帮助贝西计算农场的最大距离。

    输入输出格式

    输入格式:

    第一行:一个整数n

    第2~n+1行:xi yi 表示n个农场中第i个的坐标

    输出格式:

    一行:最远距离的[b]平方[/b]

    输入输出样例

    输入样例#1:
    4
    0 0
    0 1
    1 1
    1 0
    
    输出样例#1:
    2

    说明

    NONE

     

    想了一下还是写写旋转卡壳吧,

    毕竟做题功利性不能太强,

    但是。。。。

    旋转卡壳居然和暴力一样快,,,,,,,,,,,,,,,,,,,,

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #define vec point
     7 using namespace std;
     8 const double eps=1e-8;
     9 const int MAXN=50001;
    10 int n;
    11 void read(int &n)
    12 {
    13     char c='+';int x=0;bool flag=0;
    14     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
    15     while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}
    16     flag==1?n=-x:n=x;
    17 }
    18 inline int dcmp(double x)
    19 {
    20     if(fabs(x)<eps)    return 0;
    21     else return x>0?1:-1;
    22 }
    23 struct point
    24 {
    25     double x,y;
    26     inline point(double x=0,double y=0):x(x),y(y){};
    27 }p[MAXN],ch[MAXN];
    28 vec operator - (vec a,vec b) {return vec(a.x-b.x,a.y-b.y);}
    29 vec operator + (vec a,vec b) {return vec(a.x+b.x,a.y+b.y);}
    30 vec operator == (vec a,vec b){return (dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0);} 
    31 int comp(const point &a,const point &b)
    32 {
    33     if(a.x==b.x)    return a.y<b.y;
    34     else    return a.x<b.x;
    35 }
    36 int stack[MAXN];
    37 double cross(vec a,vec b){return a.x*b.y-a.y*b.x;}
    38 int convex_hull()
    39 {
    40     sort(p+1,p+n+1,comp);
    41     int top=1;
    42     for(int i=1;i<=n;i++)
    43     {
    44         while(top>2&& dcmp(cross(ch[top-1]-ch[top-2], p[i]-ch[top-2]))<=0)    top--;
    45         ch[top++]=p[i];
    46     }
    47     int tmp=top+1;
    48     for(int i=n-1;i>=1;i--)
    49     {
    50         while(top+1>tmp&& dcmp(cross(ch[top-1]-ch[top-2], p[i]-ch[top-2]))<=0)    top--;
    51         ch[top++]=p[i];
    52     }
    53     if(n>2) top--;
    54     return top;
    55 }
    56 int dis(point a,point b)
    57 {
    58     return    ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    59 }
    60 int num=0;
    61 int ans=0;
    62 int Cross(point a,point b,point c)
    63 {
    64     return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); 
    65 }
    66 inline void xzqk()
    67 {
    68     if(num==2)    ans=dis(ch[1],ch[2]);
    69     int j=3;
    70     for(int i=1;i<=num;i++)
    71     {
    72         while(Cross(ch[i],ch[i+1],ch[j])<Cross(ch[i],ch[i+1],ch[j+1]))
    73             j=(j+1)%num;// 防止溢出 
    74         ans=max(ans,max(dis(ch[i],ch[j]),dis(ch[i+1],ch[j])));
    75     }
    76 }
    77 int main()
    78 {
    79     //freopen("fc.in","r",stdin);
    80     //freopen("fc.out","w",stdout);
    81     read(n);
    82     //ios::sync_with_stdio(0);
    83     for(int i=1;i<=n;i++)
    84     {
    85         double a,b;
    86         cin>>a>>b;
    87         p[i]=point(a,b);
    88     }
    89     num=convex_hull();
    90     xzqk();
    91     
    92     printf("%d",ans);
    93     return 0;
    94 }
  • 相关阅读:
    BCTF赛后
    有道前端面试总结
    来更新一篇blog吧
    好久没更新了,开学新气象
    leetcode--N-Queens II
    leetcode--N-Queens
    leetcode--Scramble String
    leetcode--Maximal Rectangle
    leetcode--Distinct Subsequences
    leetcode--Sudoku Solver
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/7354462.html
Copyright © 2011-2022 走看看