11
23
2015
0

POJ3348 -- Cows

Cows
 

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

Source

 
 
 
题目大意:给出N棵树的坐标,现在用一根绳过这些树围一片草场,使得可放养的牛数量最多(一头牛需要50平米土地),求最多放养数。
 
显然的凸包,再求面积~
 
 
 
wy在这里写了Graham扫描算法的两种方式:水平序扫描法和极角序扫描法
 
水平序扫描法:
            将点按x坐标为第一关键字,y坐标为第二关键字排序
            从头到尾扫一遍求出上凸壳,再从尾到头扫一遍求出下凸壳
            需要注意的是,第二次从尾到头扫时需忽略在上凸壳中的点
 
 
极角序扫描法:
            先找出左下角的点,然后将剩下的点按照极角序排序
            (极角序相同的点按横坐标排序)
            扫一遍排序后的点集,即可求出凸包
 
                                                                                                                     ——详见 计算几何教程
 
 
水平序扫描法排序简单,但要扫描两遍
极角序扫描法只要扫描一次,但排序略复杂
至于到底哪种扫描法合适,还看个人喜好~~   O(∩_∩)O
 
#include<cstdio>
#include<algorithm>
using namespace std;
const int MaxN=10010;
int N;
int stack[MaxN],st;
bool f[MaxN];
double area;
struct point{
	double x,y;
	point (double a=0,double b=0){
		x=a; y=b;
	}
	point operator - (const point &b) const {
	    point c;
	    c.x=x-b.x;
	    c.y=y-b.y;
	    return c;
	} 
	double operator * (const point &b) const {
	    return x*b.y-b.x*y;
	}
}P[MaxN];
bool cmp(const point &a,const point &b){
	return a.x<b.x || a.x==b.x && a.y<b.y; 
}
double Abs(double x){
	return x<0?-x:x;
}
int main(){
	scanf("%d",&N);
	int k=1; 
	for (int i=1;i<=N;++i)
	    scanf("%lf%lf",&P[i].x,&P[i].y);
	sort(P+1,P+N+1,cmp);
	st=0;
	stack[++st]=1; stack[++st]=2;
	for (int i=3;i<=N;++i){
		for (;st>1&&(P[i]-P[stack[st]])*(P[stack[st]]-P[stack[st-1]])>=0;)
			f[stack[st--]]=false;
		stack[++st]=i;
		f[i]=true;
	}
	for (int i=N;i;--i)
	    if (!f[i]){
			for (;st>1&&(P[i]-P[stack[st]])*(P[stack[st]]-P[stack[st-1]])>=0;)
				f[stack[st--]]=false;
			stack[++st]=i;
			f[i]=true;
    	}
	stack[st+1]=stack[1];
	area=0;
	for (int i=2;i<st;++i)
	    area+=((P[stack[i]]-P[1])*(P[stack[i+1]]-P[1]));
	area/=100;
	printf("%d\n",(int)Abs(area));
}
#include<cstdio>
#include<algorithm>
using namespace std;
const int MaxN=10010;
int N;
int stack[MaxN],st;
double area;
struct point{
	double x,y;
	point (double a=0,double b=0){
		x=a; y=b;
	}
	point operator - (const point &b) const {
	    point c;
	    c.x=x-b.x;
	    c.y=y-b.y;
	    return c;
	} 
	double operator * (const point &b) const {
	    return x*b.y-b.x*y;
	}
}P[MaxN];
bool cmp(const point &a,const point &b){
	return (a-P[1])*(b-P[1])>0 || (a-P[1])*(b-P[1])==0 && a.x<b.x; 
}
double Abs(double x){
	return x<0?-x:x;
}
int main(){
	scanf("%d",&N);
	int k=1; 
	for (int i=1;i<=N;++i){ 
	    scanf("%lf%lf",&P[i].x,&P[i].y);
		if (P[i].x<P[k].x||P[i].x==P[k].x&&P[i].y<P[k].y)
		    k=i;
	} 
	swap(P[1],P[k]);
	sort(P+2,P+N+1,cmp);
	st=0;
	stack[++st]=1; stack[++st]=2;
	for (int i=3;i<=N;++i){
		for (;st>1&&(P[i]-P[stack[st]])*(P[stack[st]]-P[stack[st-1]])>=0;--st);
		stack[++st]=i;
	}
	stack[st+1]=stack[1];
	area=0;
	for (int i=2;i<st;++i)
	    area+=((P[stack[i]]-P[1])*(P[stack[i+1]]-P[1]));
	area/=100;
	printf("%d\n",(int)Abs(area));
}
 
Ps: 这道题有一要注意的细节,答案要用去尾法,然后————没有了...
Category: POJ | Tags: 计算几何 POJ | Read Count: 409

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com