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 Sample Output Source |
[Submit] [Go Back] [Status] [Discuss]
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator
直接上计算几何和凸包计算的板子就可以了。
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
double EPS=1e-10;
int n;
//考虑误差的加法计算
double add(double a,double b){
if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
return a+b;
}
//二维向量结构体
struct P{
double x,y;
P() {};
P(double x,double y) : x(x),y(y){
}
P operator +(P p){
return P(add(x,p.x),add(y,p.y));
}
P operator - (P p){
return P(add(x,-p.x),add(y,-p.y));
}
P operator * (double d){
return P(x*d,y*d);
}
double dot(P p){ //内积
return add(x*p.x,y*p.y);
}
double det(P p){ //外积
return add(x*p.y,-y*p.x);
}
};
P p[100005];
P qs[100005];
bool cmp(const P&p,const P&q){
if(p.x!=q.x) return p.x<q.x;
return p.y<q.y;
}
//求凸包
int convex_hull(P* p,int n,P* qs){
sort(p,p+n,cmp);
int m=0;//凸包中的定点数
//凸包下侧
for(int i=0;i<n;i++){
while(m>1&&(qs[m-1]-qs[m-2]).det(p[i]-qs[m-2])<=0) m--;
qs[m++]=p[i];
}
//构造凸包上侧
int k=m;
for(int i=n-2;i>=0;i--){
while(m>k&&(qs[m-1]-qs[m-2]).det(p[i]-qs[m-2])<=0) m--;
qs[m++]=p[i];
}
if(n>1) m--;
return m;
}
//计算多边形面积
double PolygonArea(P* q,int n){
double area=0;
for(int i=1;i<n;i++){
area=area+(q[i]-q[0]).det(q[i+1]-q[0]);
}
return fabs(area/2);
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n<3){
printf("0\n");
continue;
}
for(int i=0;i<n;i++){
scanf("%lf %lf",&p[i].x,&p[i].y);
}
int s=convex_hull(p,n,qs);
if(s<3){
printf("0\n");
continue;
}
double area=PolygonArea(qs,s);
area=area/50;
//printf("%lf\n",area);
printf("%lld\n",(long long)area);
}
return 0;
}