题意给出n个点求凸包,然后求出凸包面积就可以。
模板题求出凸包后用叉积求出面积就可以了。
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef int PointType; struct point { PointType x,y; int num; }; point data[10005],stack[10005],MinA; int top; PointType Direction(point pi,point pj,point pk) //判断向量PiPj在向量PiPk的顺逆时针方向 +顺-逆0共线 { return (pj.x-pi.x)*(pk.y-pi.y)-(pk.x-pi.x)*(pj.y-pi.y); } PointType Dis(point a,point b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } bool cmp(point a,point b) { PointType k=Direction(MinA,a,b); if(k>0) return 1; if(k<0) return 0; return Dis(MinA,a)>Dis(MinA,b); } int ab(int a) { return a>=0?a:-a; } 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[i],a[0]); 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--; stack[top++]=a[i]; } } int main() { int n; while(~scanf("%d",&n)) { for(int i=0; i<n; i++) scanf("%d%d",&data[i].x,&data[i].y); if(n<3) { puts("0"); continue; } Graham_Scan(data,n); int sum=0; for(int i=1; i<top-1; i++) sum+=ab(Direction(stack[0],stack[i],stack[i+1])); printf("%d\n",sum/100); } return 0; }