POJ 3348 Cows(凸包面积)

Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7515   Accepted: 3418

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 yseparated 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棵树,能够用这n棵树围一个圈。然后在圈里面能够养牛,每一个牛须要50平方米的空间,问最多能够养多少牛?

题解:求出凸包面积除以50就可以。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define INF 0x3f3f3f3f
#define _sign(x) ((x)>eps?1:((x)<-eps?2:0))

using namespace std;
const int MAXN = 1030;

struct Point {
    double x,y;
    Point() {}
    Point(double _x,double _y) {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &b)const {
        return Point(x - b.x,y - b.y);
    }
//叉积
    double operator ^(const Point &b)const {
        return x*b.y - y*b.x;
    }
//点积
    double operator *(const Point &b)const {
        return x*b.x + y*b.y;
    }
//绕原点旋转角度B(弧度值)。后x,y的变化
    void transXY(double B) {
        double tx = x,ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
};
Point L[MAXN];
int Stack[MAXN],top;
int n;

double dist(Point a,Point b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double multi(Point p1, Point p2, Point p3) {
    return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
}
int sgn(double x) {
    if(fabs(x)<1e-12)return 0;
    if(x<0)return -1;
    return 1;
}

//相对于L[0]的极角排序
bool _cmp(Point p1,Point p2) {
    double tmp =(p1-L[0])^(p2-L[0]);
    if(sgn(tmp)>0)return true;
    else if(sgn(tmp)==0&&sgn(dist(p1,L[0])-dist(p2,L[0]))<= 0)
        return true;
    else return false;
}

void Graham(int m) {
    if(m<=1)return;
    Point p0;
    int k = 0;
    p0 = L[0];
//找最下边的一个点
    for(int i=1; i<m; i++) {
        if((p0.y>L[i].y)||(p0.y==L[i].y&&p0.x>L[i].x)) {
            p0 = L[i];
            k = i;
        }
    }
    swap(L[k],L[0]);
    sort(L+1,L+m,_cmp);
    if(m==1) {
        top=1,Stack[0] = 0;
        return;
    }
    if(m==2) {
        top=2,Stack[0]=0,Stack[1]=1;
        return ;
    }
    Stack[0]=0;
    Stack[1]=1;
    top=2;
    for(int i=2; i<m; i++) {
        while(top>1&&sgn((L[Stack[top-1]]-L[Stack[top-2]])^(L[i]-L[Stack[top-2]]))<=0)
            top--;
        Stack[top++] = i;
    }
}

double area(Point a,Point b,Point c) {
    double la=dist(a,b);
    double lb=dist(b,c);
    double lc=dist(c,a);
    double p=(la+lb+lc)/2;
    return sqrt(p*(p-la)*(p-lb)*(p-lc));
}

int main() {
    //freopen("test.in","r",stdin);
    while(cin>>n) {
        for(int i=0; i<n; i++) {
            scanf("%lf%lf",&L[i].x,&L[i].y);
        }
        Graham(n);
        double ans=0;

        for(int i=1; i<top-1; i++) {
            int u=Stack[i],v=Stack[i+1];
            ans+=area(L[0],L[u],L[v]);
        }
        ans/=50;
        printf("%d\n",(int)ans);
    }
    return 0;
}


posted @ 2018-03-13 14:34  zhchoutai  阅读(197)  评论(0编辑  收藏  举报