POJ 3348 Cows (凸包+求面积)

Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7869   Accepted: 3565

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 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个点,围成一个最大的牧场,然后一个奶牛需要50平方米的生存空间,求最多能有多少奶牛

思路:先求出一个凸包,然后按照求多边形面积的方法求出面积然后除以50就行了。。

ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 60001
#define LL long long
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(double(-1))
using namespace std;
struct s
{
	double x,y;
}list[MAXN];
int stack[MAXN],top;
double area(s p0,s p1,s p2)
{
	return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y))/2;
}
double dis(s aa,s bb)
{
	return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));
}
int cross(s p0,s p1,s p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(s aa,s bb)
{
	int num=cross(list[0],aa,bb);
	if(num>0) return true;
	else if(num==0&&dis(list[0],aa)<dis(list[0],bb)) return true;
	else return false;
}
void graham(int n)
{
	int i;
	if(n==1) {top=0;stack[0]=0;}
	if(n==2) {top=1;stack[0]=0;stack[1]=1;}
	if(n>2)
	{
		for(i=0;i<2;i++)
		stack[i]=i;
		top=1;
		for(i=2;i<n;i++)
        {
            while(top>0&&cross(list[stack[top-1]],list[stack[top]],list[i])<=0) top--;
            top++;
            stack[top]=i;
        }  
	}
}
int main()
{
	int n,i;
	while(scanf("%d",&n)!=EOF)
	{
		s low;
		scanf("%lf%lf",&list[0].x,&list[0].y);
		low.x=list[0].x,low.y=list[0].y;
		int k=0;
		for(i=1;i<n;i++)
		{
			scanf("%lf%lf",&list[i].x,&list[i].y);
			if(((low.y==list[i].y)&&(low.x>list[i].x))||low.y>list[i].y)
			{
				low.x=list[i].x;
				low.y=list[i].y;
				k=i;
			}
		}
		list[k]=list[0];
		list[0]=low;
		sort(list+1,list+n,cmp);
		graham(n);
		double M=0;
		for(i=2;i<=top;i++)
		{
			double k=area(list[stack[0]],list[stack[i-1]],list[stack[i]]);
		    M+=k;
		}
		int ans=(int)(M/50);
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(POJ 3348 Cows (凸包+求面积))