【POJ 3348】Cows(Andrew求凸包法)

https://blog.youkuaiyun.com/linkfqy/article/details/72861176

#include<iostream>
#include<cmath>
#include<algorithm>
#define N 10005
#define eps 1e-6
using namespace std;
int n;
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
}p[N];
typedef Point Vector;
Vector operator +(Point a,Vector b)
{
    return Point(a.x+b.x,a.y+b.y);
}
Vector operator -(Point a,Point b)
{
    return Point(a.x-b.x,a.y-b.y);
}
Vector operator *(Vector a,double k)
{
    return Point(a.x*k,a.y*k);
}
double Dot(Vector a,Vector b)
{
    return (a.x*b.x)+(a.y*b.y);
}
double Cross(Vector a,Vector b)
{
    return (a.x*b.y)-(a.y*b.x);
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
inline bool cmp(const Point &a,const Point &b)
{
    if(fabs(a.x-b.x)>eps)   return a.x<b.x;
    else    return a.y<b.y;
}

int sta[N];
void Andrew()
{
    sta[1]=1;
    int top=1;
    for(int i=2;i<=n;i++)
    {
        while(top>1&&(Cross(p[i]-p[sta[top-1]],p[sta[top]]-p[sta[top-1]])<eps)) top--;
        sta[++top]=i;
    }
    int k=top;
    for(int i=n-1;i>=1;i--)
    {
        while(top>k&&(Cross(p[i]-p[sta[top-1]],p[sta[top]]-p[sta[top-1]])<eps)) top--;
        sta[++top]=i;
    }
    double ans=0;
    for(int i=1;i<top;i++)
    {
        ans+=Cross(p[sta[i]]-p[sta[1]],p[sta[i+1]]-p[1]);
    }
    cout<<abs(int(ans/100))<<endl;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>p[i].x>>p[i].y;        
    }
    sort(p+1,p+n+1,cmp);
    Andrew();
    return 0;
}

转载于:https://www.cnblogs.com/Patrickpwq/articles/9808850.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值