关闭

[COGS896]圈奶牛(凸包)

142人阅读 评论(0) 收藏 举报
分类:

题目描述

传送门

题解

凸包裸题,求凸包周长

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 10005

const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    bool operator < (const Vector &a) const
    {
        return x<a.x||(x==a.x&&y<a.y);
    }
};
typedef Vector Point;
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}

int n,top;
double x,y,ans;
Point p[N],stack[N];

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));
}
void Graham()
{
    sort(p+1,p+n+1);
    for (int i=1;i<=n;++i)
    {
        while (top>1&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
            --top;
        stack[++top]=p[i];
    }
    int k=top;
    for (int i=n-1;i>=1;--i)
    {
        while (top>k&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
            --top;
        stack[++top]=p[i];
    }
    if (n>1) --top;
}
int main()
{
    freopen("fc.in","r",stdin);
    freopen("fc.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;++i)
    {
        scanf("%lf%lf",&x,&y);
        p[i]=Point(x,y);
    }
    Graham();
    for (int i=1;i<=top;++i)
        ans+=Len(stack[i]-stack[i%top+1]);
    printf("%.2lf\n",ans);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:474856次
    • 积分:16162
    • 等级:
    • 排名:第674名
    • 原创:1151篇
    • 转载:7篇
    • 译文:0篇
    • 评论:151条
    联系方式
    QQ:1209628538 加好友请备注省份和ID
    E-mail:fiona_2000@126.com
    最新评论