poj3348 Cows 凸包 叉积求多边形面积

graham扫描法,参考yyb

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n, din;
const double eps=1e-7;
struct Point{
    double x, y;
    Point(double u=0.0, double v=0.0){
        x = u; y = v;
    }
    double crs(Point u){
        return x*u.y-y*u.x;
    }
    Point operator-(const Point &u)const{
        return Point(x-u.x, y-u.y);
    }
    double norm(){
        return sqrt(x*x+y*y);
    }
}pt[10005], sta[10005];
bool cmp(Point u, Point v){
    double tmp=(u-pt[1]).crs(v-pt[1]);
    if(fabs(tmp)<eps)   return u.norm()<v.norm();
    else    return tmp>0;
}
int main(){
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%lf %lf", &pt[i].x, &pt[i].y);
    int ps=1;
    for(int i=2; i<=n; i++)
        if(pt[i].x<pt[ps].x || (pt[i].x==pt[ps].x && pt[i].y<pt[ps].y))
            ps = i;
    swap(pt[1], pt[ps]);
    sort(pt+2, pt+1+n, cmp);
    sta[++din] = pt[1];
    for(int i=2; i<=n; i++){
        while(din>=2 && (pt[i]-sta[din-1]).crs(sta[din]-sta[din-1])>=-eps)
            din--;
        sta[++din] = pt[i];
    }
    sta[din+1] = sta[1];
    double ans=0;
    for(int i=1; i<=din; i++)
        ans += sta[i].crs(sta[i+1]);
    ans /= 2;
    printf("%d\n", (int)ans/50);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/poorpool/p/9097012.html