Cows (凸包)

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
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
struct point{
	int x,y;
}A[10010],result[10010];   //设置点 
int dist(point a,point b){
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}                     //点a,b之间的距离的平方 
int cp(point p1,point p2,point p3){
	return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}               //向量p1p3和p1p2的叉乘 
bool cmp(point a,point b){
	int ans=cp(A[0],a,b);
	if(ans==0)       //A[0]b和A[0]a共线 
		return dist(A[0],a)-dist(A[0],b)<=0;  //共线的话判断a[0]b>a[0]a? 1:0; 
	else 
		return ans>0;     //不共线的话a[0]b在a[0]a的逆时针方向ans>0返回值为1; 
}                        //排序后点相对a[0]逆时针转; 
int main()
{
	int n,i,j;
	while(scanf("%d",&n)!=EOF){
		int pos=0;
		for(i=0;i<n;++i){
			scanf("%d%d",&A[i].x,&A[i].y);
			if(A[pos].y>=A[i].y){
				if(A[pos].y==A[i].y){
					if(A[pos].x>A[i].x)pos=i;
				}
				else pos=i;
			}
		}             //找到最上面的那个点pos;pos肯定在凸包上; 
		if(n<3){
			printf("0\n");
			continue;
		}
		point temp;int top=1;
		temp=A[0];A[0]=A[pos];A[pos]=temp;  //把A[0]和A[pos]换位置 
		sort(A+1,A+n,cmp); //除了pos那个点其它的进行排序 
		result[0]=A[0];
		result[1]=A[1];
		for(i=2;i<n;++i){
			while(cp(result[top-1],result[top],A[i])<0)top--;//top从1开始 如果 1,3向量在1,2的顺时针方向删去1,2直到下一个向量在逆时针 
			result[++top]=A[i];    //存入逆时针 
		}
		double s=0;
		for(i=0;i<=top;++i){
			double area=(result[(i+1)%(top+1)].x*result[i].y-result[(i+1)%(top+1)].y*result[i].x)/2.0;//分成top+1个三角形求面积 
			s+=area;
		}
		printf("%d\n",(int)(s/50.0));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/miku531/article/details/81222146