木其网络科技专业程序员代写http://www.xmsydw.com
程序员学历擅长经验网店链接
apenny硕士ASP.NET PHP 电子 通信设计 图像 编程 网络5年进入店铺
zheng_qianqian本科C语言 C++面向对象 Java3年进入店铺
guoguanl本科Java Web项目 JSP Hibernate Struts Mysql4年进入店铺

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;
}

posted @ 2013-07-25 08:10  C语言程序  阅读(213)  评论(0编辑  收藏  举报
木其网络科技专业程序员代写http://www.xmsydw.com
程序员学历擅长经验网店链接
apenny硕士ASP.NET PHP 电子 通信设计 图像 编程 网络5年进入店铺
zheng_qianqian本科C语言 C++面向对象 Java3年进入店铺
guoguanl本科Java Web项目 JSP Hibernate Struts Mysql4年进入店铺