旋转卡壳 求凸多边形中面积最大的四边形
枚举对角线,用旋转卡壳求对角线两侧的最大的三角形。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #include<cstdlib> 7 using namespace std; 8 struct data 9 { 10 double x,y; 11 }a[1005]; 12 double get(data p1,data p2,data p3) 13 { 14 return fabs((p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y))/2; 15 } 16 double max1,max2,ans,ans1; 17 int n; 18 int main() 19 { 20 freopen("quads.in","r",stdin); 21 freopen("quads.out","w",stdout); 22 scanf("%d",&n); 23 for(int i=0;i<n;i++) scanf("%lf%lf",&a[i].x,&a[i].y); 24 for(int i=0;i<n-2;i++) 25 { 26 int j=(i+2)%n,k=(i+1)%n,h=(j+1)%n; 27 while(get(a[i],a[j],a[k+1])>get(a[i],a[j],a[k])) k=(k+1)%n; 28 max1=get(a[i],a[j],a[k]); 29 while(get(a[i],a[j],a[h+1])>get(a[i],a[j],a[h])) h=(h+1)%n; 30 max2=get(a[i],a[j],a[h]); 31 ans1=0; 32 while(max1+max2>ans1&&j<n-1) 33 { 34 ans1=max1+max2; 35 j=(j+1)%n; 36 while(get(a[i],a[j],a[k+1])>get(a[i],a[j],a[k])) k=(k+1)%n; 37 max1=get(a[i],a[j],a[k]); 38 while(get(a[i],a[j],a[h+1])>get(a[i],a[j],a[h])) h=(h+1)%n; 39 max2=get(a[i],a[j],a[h]); 40 } 41 ans=max(ans,ans1); 42 } 43 printf("%.10lf\n",ans); 44 return 0; 45 }
O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~