POJ 2187

题意:给你一些点,求最远点对

思路:先求凸包,然后旋转卡壳模板,不断枚举边,找凸包上的最远点,更新答案

感谢:http://hzwer.com/4224.html

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<set>
  6 #include<ctime>
  7 #include<vector>
  8 #include<queue>
  9 #include<algorithm>
 10 #include<map>
 11 #include<cmath>
 12 #define eps 1e-8
 13 #define inf 1000000000
 14 #define pa pair<int,int>
 15 #define ll long long 
 16 using namespace std;
 17 int read()
 18 {
 19     int x=0,f=1;char ch=getchar();
 20     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 21     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 22     return x*f;
 23 }
 24 int n,top;
 25 double ans;
 26 double sqr(double x)
 27 {
 28 
 29     return x*x;
 30 }
 31 struct P{
 32     double x,y;
 33     P(){}
 34     P(double _x,double _y):x(_x),y(_y){}
 35     friend P operator +(P a,P b){
 36         return P(a.x+b.x,a.y+b.y);
 37     }
 38     friend P operator -(P a,P b){
 39         return P(a.x-b.x,a.y-b.y);
 40     }
 41     friend double operator*(P a,P b){
 42         return a.x*b.y-a.y*b.x;
 43     }
 44     friend double operator/(P a,P b){
 45         return a.x*b.x+a.y*b.y;
 46     }
 47     friend bool operator==(P a,P b){
 48         return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;
 49     }
 50     friend bool operator!=(P a,P b){
 51         return !(a==b);
 52     }
 53     friend bool operator<(P a,P b){
 54         if(fabs(a.y-b.y)<eps)return a.x<b.x;
 55         return a.y<b.y;
 56     }
 57     friend double dis2(P a){
 58         return sqr(a.x)+sqr(a.y);
 59     }
 60     friend void print(P a){
 61         printf("%.2lf %.2lf
",a.x,a.y);
 62     }
 63 }p[50005],q[50005];
 64 bool cmp(P a,P b)
 65 {
 66     if(fabs((b-p[1])*(a-p[1]))<eps)return dis2(a-p[1])<dis2(b-p[1]);
 67     return (a-p[1])*(b-p[1])>0;
 68 }
 69 void graham()//求求凸包  原来点用p存 求完凸包放在q里面
 70 {
 71     for(int i=1;i<=n;i++)
 72         if(p[i]<p[1])swap(p[i],p[1]);
 73     sort(p+2,p+n+1,cmp);
 74     q[++top]=p[1];q[++top]=p[2];
 75     for(int i=3;i<=n;i++)
 76     {
 77         while((q[top]-q[top-1])*(p[i]-q[top-1])<eps&&top>1)top--;
 78         q[++top]=p[i];
 79     }
 80 }
 81 void RC()
 82 {
 83     q[top+1]=q[1];
 84     int now=2;
 85     for(int i=1;i<=top;i++)//枚举边
 86     {
 87         while((q[i+1]-q[i])*(q[now]-q[i])<(q[i+1]-q[i])*(q[now+1]-q[i]))
 88         {
 89             now++;
 90             if(now==top+1)now=1;
 91         }//q[now]为凸包上距离当前边最远的点
 92         ans=max(ans,dis2(q[now]-q[i]));
 93     }
 94 }
 95 int main()
 96 {
 97     n=read();
 98     for(int i=1;i<=n;i++)
 99         p[i].x=read(),p[i].y=read();
100     graham();
101     RC();
102     printf("%d",(int)ans);
103     return 0;
104 }
原文地址:https://www.cnblogs.com/agenthtb/p/7684124.html