POJ 3348 Cows 求凸包面积
套模板再算下面积即可
#include<stdio.h> #include<math.h> #include<algorithm> #include<iostream> using namespace std; const int MAXN=10008; const double PI = acos(-1); struct Point { int x,y; Point() {}; Point(int xx, int yy) { x = xx, y = yy; } }; Point pot[MAXN]; int stck[MAXN],top; int cross(Point p0,Point p1,Point p2) //计算叉积 p0p1 X p0p2 { return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); } double dis(Point p1,Point p2) //计算 p1p2的 距离 { return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)); } bool cmp(Point p1,Point p2) //极角排序函数,角度相同则距离小的在前面 { int tmp=cross(pot[0],p1,p2); if(tmp>0) return true; else if(tmp==0&&dis(pot[0],p1)<dis(pot[0],p2)) return true; else return false; } void init(int n) //输入,并把最左下方的点放在 pot[0]。并且进行极角排序 { int i,k; Point p0; p0 = pot[0]; k=0; for(i=1; i<n; i++) { if( (p0.y>pot[i].y) || ((p0.y==pot[i].y)&&(p0.x>pot[i].x)) ) { p0 = pot[i]; k=i; } } pot[k]=pot[0]; pot[0]=p0; sort(pot+1,pot+n,cmp); } void graham(int n) { int i; if(n==1) { top=0; stck[0]=0; } if(n==2) { top=1; stck[0]=0; stck[1]=1; } if(n>2) { for(i=0; i<=1; i++) stck[i]=i; top=1; for(i=2; i<n; i++) { while(top>0&&cross(pot[stck[top-1]],pot[stck[top]],pot[i])<=0) top--; top++; stck[top]=i; } } } int main() { // freopen("in.txt","r",stdin); int n; while(~scanf("%d", &n)) { for(int i=0;i<n;i++) scanf("%d%d", &pot[i].x, &pot[i].y); init(n); graham(n); stck[++top] = stck[0]; int ans = 0; for(int i=0; i<top; i++) ans += cross(pot[0], pot[stck[i]], pot[stck[i+1]]); printf("%d\n", ans / 100); } return 0; }