[POJ3348]Cows 凸包模板题

Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10262   Accepted: 4510

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


如果不会凸包模板的看这里:凸包的模板

题解:

本题也是凸包的一个模板题,很显然把木桩看成平面上的点,求出凸包的几个点,用叉积算出面积。因为每头牛要占用50平方米的面积,所以答案除以50再下取整即可。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int Maxn=100005;
struct node{
	int x,y;
}a[Maxn],ans[Maxn];
int n,k,lim;
int operator *(const node &a,const node &b){
	return a.x*b.y-a.y*b.x;
}
node operator -(const node &a,const node &b){
	node c;
	c.x=a.x-b.x;c.y=a.y-b.y;
	return c;
}
inline double dis(node a,node b){
	return sqrt(double((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
} 
bool cmp(node x,node y){
	if(x.y!=y.y)return x.y<y.y;
	return x.x<y.x;
}
bool check(node o,node x,node y){
	return (x-o)*(y-o)>0?0:1;
}
void work(){
	k=0;
	double sum=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].y);
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++){
		while(k>=2&&check(ans[k-1],ans[k],a[i]))k--;
		ans[++k]=a[i];
	}
	lim=k;
	for(int i=n;i>=1;i--){
		while(k>=lim+1&&check(ans[k-1],ans[k],a[i]))k--;
		ans[++k]=a[i];
	}
	if(n>1)k--;
	for(int i=1;i<k;i++){
		sum+=ans[i]*ans[i+1];
	}
	sum+=ans[k]*ans[1];
	printf("%.0lf\n",floor(sum/100.0));
}
int main(){
	work();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lvyanchang/article/details/80376328