2018.07.03 POJ 3348 Cows

Cows
Time Limit: 2000MS Memory Limit: 65536K
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
CCC 2007

很显然,这道题是要让我们算出给出点的凸包的面积。综上所诉:这是一道凸包的裸板题,我本蒟蒻用的是 G r a h a m 扫描法。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 10005
using namespace std;
struct pot{
    double x,y;
}p[N],vec[N];
inline double cross(pot a,pot b){return a.x*b.y-a.y*b.x;}
inline pot Vector(pot a,pot b){pot c;c.x=a.x-b.x,c.y=a.y-b.y;return c;}
inline double calc(pot *p,int n){
    double ans=0;
    for(int i=1;i<n-1;++i)
        ans+=cross(Vector(p[i],p[0]),Vector(p[i+1],p[0]));
    return ans/2;
}
inline bool cmp(const pot&x,const pot&y){return x.x==y.x?x.y<=y.y:x.x<y.x;}
inline int solve(pot *p,int n,pot *vec){
    sort(p,p+n,cmp);
    int m=0;
    for(int i=0;i<n;++i){
        while(m>1&&cross(Vector(vec[m-1],vec[m-2]),Vector(p[i],vec[m-2]))<=0)--m;
        vec[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;--i){
        while(m>k&&cross(Vector(vec[m-1],vec[m-2]),Vector(p[i],vec[m-2]))<=0)--m;
        vec[m++]=p[i];
    }
    if(n>1)m--;
    return m;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;++i)scanf("%lf%lf",&p[i].x,&p[i].y);
    int m=solve(p,n,vec),ans=0;
    double area=calc(vec,m);
    while(area>=50)area-=50,++ans;
    printf("%d",ans);
}

猜你喜欢

转载自blog.csdn.net/dreaming__ldx/article/details/80897042