POJ 3348 Cows 凸包面积
题目大意:连接个点,求围成的面积并除以50(向下取整)
题目思路:就是求凸包面积:按逆时针方向为凸包上每条边指定方向,对于每条边AB,累加(AXB)/2的值。
#include<cstdio> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<queue> #define INF 0x3f3f3f3f #define MAX 100005 using namespace std; struct node { int x,y,id; }point[MAX]; int n,ans[MAX],Stuck[MAX],top; bool cmp(struct node A,struct node B) { if(A.x < B.x) return true; else if(A.x==B.x && A.y < A.y) return true; return false; } int Cross(int x1,int y1,int x2,int y2,int x3,int y3) { return (x1-x2)*(y1-y3)-(x1-x3)*(y1-y2); } double Area() { double sum=0.0; for(int i=1;i<=top;i++) sum+=(point[Stuck[i-1]].x*point[Stuck[i]].y-point[Stuck[i]].x*point[Stuck[i-1]].y); return fabs(sum/2); } void Graham() { int len; top=1; for(int i=0;i<2;i++) Stuck[i]=i; for(int i=2;i<n;i++) { while(top>0 && Cross(point[Stuck[top]].x,point[Stuck[top]].y,point[Stuck[top-1]].x,point[Stuck[top-1]].y,point[i].x,point[i].y)<=0) top--; Stuck[++top]=i; } len=top; Stuck[++top]=n-2; for(int i=n-3;i>=0;i--) { while(top!=len && Cross(point[Stuck[top]].x,point[Stuck[top]].y,point[Stuck[top-1]].x,point[Stuck[top-1]].y,point[i].x,point[i].y)<=0) top--; Stuck[++top]=i; } } int main() { while(scanf("%d",&n)!=EOF) { memset(ans,0,sizeof(ans)); for(int i=0;i<n;i++) { scanf("%d%d",&point[i].x,&point[i].y); point[i].id=i; } if(n<3) { printf("0\n"); continue; } sort(point,point+n,cmp); Graham(); double ans=Area(); int sum=ans/50; printf("%d\n",sum); } return 0; }