求凸包面积:poj3348

Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6299 Accepted: 2880

Description

Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are forced to save money on buying fence posts by using trees as fence posts wherever possible. Given the locations of some trees, you are to help farmers try to create the largest pasture that is possible. Not all the trees will need to be used.

However, because you will oversee the construction of the pasture yourself, all the farmers want to know is how many cows they can put in the pasture. It is well known that a cow needs at least 50 square metres of pasture to survive.

Input

The first line of input contains a single integer, n (1 ≤ n ≤ 10000), containing the number of trees that grow on the available land. The next n lines contain the integer coordinates of each tree given as two integers x and yseparated by one space (where -1000 ≤ x, y ≤ 1000). The integer coordinates correlate exactly to distance in metres (e.g., the distance between coordinate (10; 11) and (11; 11) is one metre).

Output

You are to output a single integer value, the number of cows that can survive on the largest field you can construct using the available trees.

Sample Input

4
0 0
0 101
75 0
75 101

Sample Output

151

记得以前有写过凸包求周卓的代码。求面积则是利用三角形面积是向量叉积的二分之一这个原理。效果一样。可以作为凸包的模板了。(*^__^*) 嘻嘻……

判断某点在直线的左右侧http://blog.csdn.net/modiz/article/details/9928955

定义:平面上的三点P1(x1,y1),P2(x2,y2),P3(x3,y3)的面积量:

S(P1,P2,P3)=|y1 y2 y3|= (x1-x3)*(y2-y3)-(y1-y3)*(x2-x3) 
当P1P2P3逆时针时S为正的,当P1P2P3顺时针时S为负的。 
令矢量的起点为A,终点为B,判断的点为C, 
如果S(A,B,C)为正数,则C在矢量AB的左侧; 
如果S(A,B,C)为负数,则C在矢量AB的右侧; 
如果S(A,B,C)为0,则C在直线AB上。


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define ling 1e-10
#define pi acos(-1)
struct point
{
	int x,y;
}p[10010],f[10010];
double mul(point a,point b,point c)
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int cmp(point a,point b)
{
	double k=mul(p[0],a,b);
	if (k<-ling)
		return 0;
	if (fabs(k)<=ling && dis(p[0],a)-dis(p[0],b)>ling)
		return 0;
	return 1;
}
int main()
{
	int n,i,t,m;
	while (~scanf("%d",&n))
	{
		for (i=0;i<n;i++)
		{
			scanf("%d%d",&p[i].x,&p[i].y);
			if (i==0) t=0;
			else
			if (p[t].y>p[i].y)
				t=i;
			else
			if (p[t].y==p[i].y && p[t].x>p[t].x)
				t=i;
		}
		point h=p[t];
		p[t]=p[0];
		p[0]=h;
		sort(p+1,p+n,cmp);
		int l=1,j=1;
		f[0]=p[n-1];
		f[1]=p[0];
		while (l<n)
		{
			double k=mul(p[l],f[j-1],f[j]);
			if (k>ling)
				f[++j]=p[l++];
			else j--;
		}
		//求凸包周长
	/*	double s=dis(f[0],f[j]);
		for (i=1;i<=j;i++)
			s+=dis(f[i-1],f[i]);
		printf("%d\n",(int)(s+(2*pi*m+0.5)));*/
		//求凸包面积
		double ans=0;
		for (i=1;i<=j;i++)
		{
			double k=mul(f[0],f[i],f[i+1]);
			ans+=k;
		}
		ans=fabs(ans)/2;
		printf("%d\n",(int)(ans/50));
	} 
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值