POJ 3348 /// 凸包+多边形面积

题目大意:

给定的n个点 能圈出的最大范围中

若每50平方米放一头牛 一共能放多少头

求凸包 答案就是 凸包的面积/50 向下取整

/// 求多边形面积
//
凹多边形同样适用 因为点积求出的是有向面积 double areaPg() { double res=0; for(int i=1;i<k-1;i++) // 以t[0]为划分顶点 res+=(t[i]-t[0]).det(t[i+1]-t[0]); return res/2.0; }
#include <cstdio>
#include <algorithm>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;

const double eps=1e-10;
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[10005], t[10005];
int n, k;
bool cmp(P a,P b) {
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
void andrew()
{
    sort(p,p+n,cmp);
    k=0;
    for(int i=0;i<n;i++) {
        while(k>1 && (t[k-1]-t[k-2]).det(p[i]-t[k-1])<0)
            k--;
        t[k++]=p[i];
    }
    for(int i=n-2,j=k;i>=0;i--) {
        while(k>j && (t[k-1]-t[k-2]).det(p[i]-t[k-1])<0)
            k--;
        t[k++]=p[i];
    }
    if(n>1) k--;
}
double areaPg()
{
    double res=0;
    for(int i=1;i<k-1;i++)
        res+=(t[i]-t[0]).det(t[i+1]-t[0]);
    return res/2.0;
}
void solve()
{
    andrew();
    int ans=areaPg()/50;
    printf("%d
",ans);
}

int main()
{
    while(~scanf("%d",&n)) {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        solve();
    }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zquzjx/p/9649769.html