POJ 3348 Cows

题目大意:

给你n棵树,可以用这n棵树围一个圈,然后在圈里面可以养牛,每个牛需要50平方米的空间,问最多可以养多少牛?

其实就是求一个凸包,计算凸包面积,然后除以50,然后就得到答案,直接上模板了。

凸包这一类型的题目差不多,可以作为模板使用,时间复杂度是NlogN。

 

//Time 32ms; Memory 568K
#include<iostream>
#include<algorithm>

using namespace std;

int n;

typedef struct point 
{
	double x,y;
	point(double xx=0,double yy=0):x(xx),y(yy){}
}vector;

point p[10010],ch[10010];

bool operator < (point a,point b)
{
	return a.x<b.x || (a.x==b.x && a.y<b.y);
}
vector operator - (point a,point b)
{
	return vector(a.x-b.x,a.y-b.y);
}
double cross(vector a,vector b)
{
	return a.x*b.y-a.y*b.x;
}

int graph()
{
	int k,m=0,i;
	for(i=0;i<n;i++)
	{
		while(m>1 && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	k=m;
	for(i=n-2;i>=0;i--)
	{
		while(m>k && cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	if(n>1) m--;
	return m;
}
int main()
{
	int i,m,d;
	double s=0;
	cin>>n;
	for(i=0;i<n;i++) cin>>p[i].x>>p[i].y;
	sort(p,p+n);
	m=graph();
	for(i=1;i<m-1;i++) s+=0.5*cross(ch[i]-ch[0],ch[i+1]-ch[0]);
	d=s/50;
	cout<<d<<endl;
	return 0;
}
原文地址:https://www.cnblogs.com/jiangu66/p/3215154.html