Cows - POJ 3348(凸包求面积)

题目大意:利用n棵树当木桩修建牛圈,知道每头牛需要50平的生存空间,求最多能放养多少头牛。

分析:赤裸裸的求凸包然后计算凸包的面积。

 

代码如下:

-------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;

const int MAXN = 1e4+7;
const double EPS = 1e-10;

int sta[MAXN], top;

struct point
{
    double x, y;

    point(double x=0, double y=0):x(x), y(y){}
    point operator - (const point &t)const{
        return point(x-t.x, y-t.y);
    }
    double operator *(const point &t)const{
        return x*t.x + y*t.y;
    }
    double operator ^(const point &t)const{
        return x*t.y - y*t.x;
    }
}p[MAXN];
int Sign(double t)
{
    if(t > EPS)return 1;
    if(fabs(t) < EPS)return 0;
    return -1;
}
double Dist(point a, point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(point a, point b)
{
    int t = Sign((a-p[0])^(b-p[0]));

    if(t == 0)
        return Dist(a, p[0]) < Dist(b, p[0]);
    return t > 0;
}
void Graham(int N)
{
    int k=0;

    for(int i=0; i<N; i++)
    {
        if(p[k].y>p[i].y || (Sign(p[k].y-p[i].y)==0 && p[k].x > p[i].x))
            k = i;
    }
    swap(p[0], p[k]);
    sort(p+1, p+N, cmp);

    sta[0]=0, sta[1]=1, top=1;

    if(N < 2)
    {
        top = N-1;
        return ;
    }

    for(int i=2; i<N; i++)
    {
        while(top>0 && Sign((p[i]-p[sta[top]])^(p[sta[top-1]]-p[sta[top]])) <= 0)
            top--;
        sta[++top] = i;
    }
}
int main()
{
    int N;

    while(scanf("%d", &N) != EOF && N)
    {
        for(int i=0; i<N; i++)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }

        Graham(N);

        double area=0;

        for(int i=1; i<top; i++)
        {
            double a = Dist( p[ sta[0] ], p[ sta[i] ] );
            double b = Dist( p[ sta[0] ], p[ sta[i+1] ] );
            double c = Dist( p[ sta[i] ], p[ sta[i+1] ] );
            double q = (a+b+c) / 2;

            area += sqrt(q*(q-a)*(q-b)*(q-c));
        }

        printf("%d\n", (int)(area/50.0));
    }

    return 0;
}

 

posted @ 2015-10-26 15:39  无忧望月  阅读(144)  评论(0编辑  收藏  举报
levels of contents