Description
在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成
的多边形面积最大。
Input
第1行一个正整数N,接下来N行,每行2个数x,y,表示该点的横坐标和纵坐标。
Output
最大的多边形面积,答案精确到小数点后3位。
Sample Input
5
0 0
1 0
1 1
0 1
0.5 0.5
0 0
1 0
1 1
0 1
0.5 0.5
Sample Output
1.000
HINT
数据范围 n<=2000, |x|,|y|<=100000
首先最大的4个点一定在凸包上
先把凸包求出来
然后选定对角线,然后发现另外两个顶点是单调的,用旋转卡壳的思想
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 struct point 8 { 9 double x,y; 10 }p[5001],s[5001]; 11 int n,top; 12 double ans,eps=1e-5; 13 int dcmp(double x) 14 { 15 if (x<-eps) return -1; 16 if (x>eps) return 1; 17 return 0; 18 } 19 double operator *(point a,point b) 20 { 21 return a.x*b.y-a.y*b.x; 22 } 23 point operator -(point a,point b) 24 { 25 return (point){a.x-b.x,a.y-b.y}; 26 } 27 double dist(point a,point b) 28 { 29 return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); 30 } 31 bool cmp(point a,point b) 32 { 33 return (a.y<b.y)||(a.y==b.y&&a.x<b.x); 34 } 35 bool cmp2(point a,point b) 36 { 37 int t=dcmp((p[1]-a)*(p[1]-b)); 38 if (t==0) return dist(p[1],a)<dist(p[1],b); 39 return t>0; 40 } 41 void graham() 42 {int i; 43 sort(p+1,p+n+1,cmp); 44 sort(p+2,p+n+1,cmp2); 45 s[++top]=p[1];s[++top]=p[2]; 46 for (i=3;i<=n;i++) 47 { 48 while (top>1&&dcmp((p[i]-s[top-1])*(s[top]-s[top-1]))>=0) top--; 49 s[++top]=p[i]; 50 } 51 } 52 void solve() 53 {int i,j; 54 s[top+1]=s[1]; 55 for (i=1;i<=top-2;i++) 56 { 57 int a=i%top+1,b=(i+2)%top+1; 58 for (j=i+2;j<=top;j++) 59 { 60 while (a%top+1!=j&&dcmp((s[a+1]-s[i])*(s[j]-s[i])-(s[a]-s[i])*(s[j]-s[i]))>0) a=a%top+1; 61 while (b%top+1!=i&&dcmp((s[j]-s[i])*(s[b+1]-s[i])-(s[j]-s[i])*(s[b]-s[i]))>0) b=b%top+1; 62 //cout<<i<<‘ ‘<<a<<‘ ‘<<j<<‘ ‘<<b<<endl; 63 ans=max(ans,(s[a]-s[i])*(s[j]-s[i])+(s[j]-s[i])*(s[b]-s[i])); 64 } 65 } 66 } 67 int main() 68 {int i; 69 cin>>n; 70 for (i=1;i<=n;i++) 71 { 72 scanf("%lf%lf",&p[i].x,&p[i].y); 73 } 74 graham(); 75 solve(); 76 printf("%.3lf\n",ans/2.0); 77 }