1. 程式人生 > >3348 Cows(Andrew演算法+凸包)

3348 Cows(Andrew演算法+凸包)

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 y separated 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

題目大意:給你n顆樹的座標,讓後讓你求這n顆數能夠圍成的最大面積,每隻奶牛需要50平方米的空間,圍成的範圍能夠生存的最大奶牛數量

解題思路:使用Andrew演算法求出最大凸包,然後使用叉乘計算凸多邊的面積,最後除以50就可以了

AC程式碼:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1e4+10;
struct point{
	int x,y;
}p[maxn],ch[maxn];//樹的座標 
bool cmp(point a,point b)
{
	if(a.x==b.x)
		return a.y<b.y;
	return a.x<b.x;
}
int det(point a,point b,point c)//叉乘 
{
	return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
int Andrew(point *p,int n,point *ch)//Andrew演算法求凸包 
{
	int m=0;
	sort(p,p+n,cmp);
	for(int i=0;i<n;i++)
	{
		while(m>1&&det(ch[m-2],ch[m-1],p[i])>=0)
			m--;
		ch[m++]=p[i];
	}
	int k=m;
	for(int i=n-2;i>=0;i--)
	{
		while(m>k&&det(ch[m-2],ch[m-1],p[i])>=0)
			m--;
		ch[m++]=p[i];
	}
	if(n>1)
		m--;
	return m;//返回凸包邊上樹的個數 
}
double area(point *ch,int m)//計算凸多邊形的面積 
{
	double sum=0;
	for(int i=1;i<m-1;i++)
	{
		sum+=fabs(1.0*det(ch[0],ch[i],ch[i+1])/2);
	}
	return sum;
} 
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>p[i].x>>p[i].y;
	int m=Andrew(p,n,ch);
	double sum=area(ch,m);
	int ans=sum/50;
	cout<<ans<<endl;
	return 0;		
}