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;
}

转载于:https://www.cnblogs.com/poorpool/p/9097012.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值