题意:给出N个点,范围小于5W,求出其中三个点围成的最大面积。
先求出凸包,可以发现最大面积三角形的顶点必然在凸包上。。然后枚举凸包上的每条边,用旋转卡壳来找出距离当前边最远的点,此时更新最大面积即可。
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define maxn 50500 struct point { double x,y; }; point data[maxn],stack[maxn],MinA; int top; double Direction(point i,point j,point k) { return (j.x-i.x)*(k.y-i.y)-(k.x-i.x)*(j.y-i.y); } double Dis(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } bool cmp(point a,point b) { double k=Direction(MinA,a,b); if(k>0)return 1; if(k<0)return 0; return Dis(MinA,a)>Dis(MinA,b); } void Graham_Scan(point *a,int numa) { for(int i=0; i<numa; i++) if(a[i].y<a[0].y||a[i].y==a[0].y&&a[i].x<a[0].x) swap(a[0],a[i]); MinA=a[0],top=0; sort(a+1,a+numa,cmp); stack[top++]=a[0],stack[top++]=a[1],stack[top++]=a[2]; for(int i=3; i<numa; i++) { while(Direction(stack[top-2],stack[top-1],a[i])<0&&top>=2) top--; stack[top++]=a[i]; } } double rotating_calipers(point *ch,int n) { double ans=0; int q=1; ch[n]=ch[0]; for(int p=0; p<n; p++) { q=(p+2)%n; for(int i=p+1; i<n; i++) { double mm; while(Direction(ch[p],ch[i],ch[q+1])>(mm=Direction(ch[p],ch[i],ch[q]))) q=(q+1)%n; ans=max(ans,mm); } } return ans/2; } int main() { int n; while(~scanf("%d",&n),n!=-1) { for(int i=0; i<n; i++) scanf("%lf%lf",&data[i].x,&data[i].y); Graham_Scan(data,n); printf("%.2f\n",rotating_calipers(stack,top)); } return 0; }