【POJ2187】Beauty Contest

大致题意:给定一个散点集,求最远点距的平方。

题解:旋转卡壳裸题

代码如下:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 #define MN 50005
 6 using namespace std;
 7 int n,top=-1,tmp;
 8 double ans;
 9 struct point{
10     double x,y;
11     point(double X=0,double Y=0){x=X,y=Y;}
12     friend bool operator <(point a,point b){
13         return a.x<b.x||(a.x==b.x&&a.y<b.y);
14     }
15 }p[MN],q[MN];
16 point operator +(point a,point b){return point(a.x+b.x,a.y+b.y);}
17 point operator -(point a,point b){return point(a.x-b.x,a.y-b.y);}
18 double cross(point a,point b){return a.x*b.y-a.y*b.x;}
19 double len(point a){return a.x*a.x+a.y*a.y;}
20 int main()
21 {
22     scanf("%d",&n);
23     for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
24     sort(p+1,p+1+n);
25     for(int i=1;i<=n;i++){
26         while(top>=1&&cross(q[top]-q[top-1],p[i]-q[top-1])<=0)top--;
27         q[++top]=p[i];
28     }tmp=top;
29     for(int i=n-1;i>=1;i--){
30         while(top>tmp&&cross(q[top]-q[top-1],p[i]-q[top-1])<=0)top--;
31         q[++top]=p[i];
32     }
33     for(int i=0;i<top;i++){
34         tmp=1;
35         while(cross(q[i+1]-q[i],q[tmp+1]-q[i])>cross(q[i+1]-q[i],q[tmp]-q[i]))tmp=(tmp+1)%top;
36         ans=max(ans,max(len(q[tmp]-q[i]),max(len(q[tmp+1]-q[i]),max(len(q[tmp]-q[i+1]),len(q[tmp+1]-q[i+1])))));
37     }
38     printf("%.0lf",ans);
39     return 0;
40 }
原文地址:https://www.cnblogs.com/Beginner-/p/8479383.html