【计算几何/凸包】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

题意

给你n个点,问这些点所能围城的最大问题是多少

题解

凸包裸题,最后用叉积求一波面积即可。

AC代码

#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;

class Point
{
    public:
        double x,y;
        Point(double x = 0,double y = 0):x(x),y(y) {}
        Point operator + (Point a)
        {
            return Point(x + a.x, y + a.y);
        }
        Point operator - (Point a)
        {
            return Point(x - a.x, y - a.y);
        }
        bool operator < (const Point &a) const
        {
            if(x == a.x) return y < a.y;
            return x < a.x;
        }
};

typedef vector<Point> Polygom;
typedef Point Vector;

double cross(Vector a,Vector b)
{
    return a.x*b.y - a.y*b.x;
}

bool isclock(Point p0,Point p1,Point p2)
{
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if(cross(a,b)< 0) return true;
    return false;
}

double getArea(Polygom a)
{
    double ans = 0;
    for(int i = 1 ; i < a.size() - 1 ; i++)
    {
        Vector x1 = a[i] - a[0];
        Vector x2 = a[i+1] - a[0];
        ans+=abs(cross(x1,x2));
    }
    return ans;
}

Polygom andrewScan(Polygom s)
{
    Polygom u,l;
    if(s.size() < 3) return s;
    sort(s.begin(),s.end());
    u.push_back(s[0]);
    u.push_back(s[1]);
    l.push_back(s[s.size()-1]);
    l.push_back(s[s.size()-2]);

    for(int i = 2 ; i < s.size() ; i++)
    {
        for(int n = u.size() ; n >= 2 && isclock(u[n-2],u[n-1],s[i])!=true ; n--)
            u.pop_back();
        u.push_back(s[i]);
    }

    for( int i = s.size() - 3 ; i >= 0 ; i--)
    {
        for(int n = l.size() ; n >= 2 && isclock(l[n-2],l[n-1],s[i])!= true ;n--)
            l.pop_back();
        l.push_back(s[i]);
    }

    for(int i = 1 ; i < l.size() - 1 ; i++) u.push_back(l[i]);
    return u;
}

void solve(void)
{
    int n;
    Polygom a;
    scanf("%d",&n);
    while(n--)
    {
        Point q;
        scanf("%lf%lf",&q.x,&q.y);
        a.push_back(q);
    }
    a = andrewScan(a);
    printf("%d",(int)getArea(a)/100);
}


int main(void)
{
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/peng0614/article/details/81154213