【POJ 3348】Cows(Andrew求凸包法)

https://blog.csdn.net/linkfqy/article/details/72861176

#include<iostream>
#include<cmath>
#include<algorithm>
#define N 10005
#define eps 1e-6
using namespace std;
int n;
struct Point
{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
}p[N];
typedef Point Vector;
Vector operator +(Point a,Vector b)
{
	return Point(a.x+b.x,a.y+b.y);
}
Vector operator -(Point a,Point b)
{
	return Point(a.x-b.x,a.y-b.y);
}
Vector operator *(Vector a,double k)
{
	return Point(a.x*k,a.y*k);
}
double Dot(Vector a,Vector b)
{
	return (a.x*b.x)+(a.y*b.y);
}
double Cross(Vector a,Vector b)
{
	return (a.x*b.y)-(a.y*b.x);
}
double Len(Vector a)
{
	return sqrt(Dot(a,a));
}
inline bool cmp(const Point &a,const Point &b)
{
	if(fabs(a.x-b.x)>eps)	return a.x<b.x;
	else	return a.y<b.y;
}

int sta[N];
void Andrew()
{
	sta[1]=1;
	int top=1;
	for(int i=2;i<=n;i++)
	{
		while(top>1&&(Cross(p[i]-p[sta[top-1]],p[sta[top]]-p[sta[top-1]])<eps))	top--;
		sta[++top]=i;
	}
	int k=top;
	for(int i=n-1;i>=1;i--)
	{
		while(top>k&&(Cross(p[i]-p[sta[top-1]],p[sta[top]]-p[sta[top-1]])<eps))	top--;
		sta[++top]=i;
	}
	double ans=0;
	for(int i=1;i<top;i++)
	{
		ans+=Cross(p[sta[i]]-p[sta[1]],p[sta[i+1]]-p[1]);
	}
	cout<<abs(int(ans/100))<<endl;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p[i].x>>p[i].y;		
	}
	sort(p+1,p+n+1,cmp);
	Andrew();
	return 0;
}
posted @ 2018-10-18 10:01  Patrickpwq  阅读(92)  评论(0编辑  收藏  举报