bzoj 1670 护城河的挖掘 凸包

#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm> 
#define maxn 5005
using namespace std;
struct point
{
    double x,y;
    point(){}
    point(double x1,double y1){x=x1,y=y1;}
    double operator %(const point &A) const
    {   return x*A.y-y*A.x;}
    double operator *(const point &A) const
    {   return x*A.x+y*A.y;}
    point operator -(const point &A) const
    {   return point(x-A.x,y-A.y);}
    point operator +(const point &A) const
    {   return point(x+A.x,y+A.y);}
};
point p[maxn],S[maxn];int top;
bool cmp(point A,point B)
{return A.x<B.x||(A.x==B.x&&A.y<B.y);}

double pw(double x){return x*x;}
double Cross(point A,point B,point C)
{return (B-A)%(C-A);}
double dis(point A,point B)
{return sqrt(pw(A.x-B.x)+pw(A.y-B.y));}

    int n;
double ans;
void get_Tubao()
{
    sort(p+1,p+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
        while(top>1&&Cross(S[top-1],p[i],S[top])<=0) top--;
        S[++top]=p[i];
    }
    int j=top;
    for(int i=n;i>=1;i--)
    {
        while(top>j&&Cross(S[top-1],p[i],S[top])<=0) top--;
        S[++top]=p[i];
    }
    for(int i=1;i<top;i++)
        ans+=dis(S[i],S[i+1]);
    ans+=dis(S[1],S[top]);
}
int main()
{
    scanf("%d",&n);
    int x,y;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&x,&y);
        p[i].x=(double)x;
        p[i].y=(double)y;
    }
    get_Tubao();
    printf("%.2lf",ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值