hdu 1392 Surround the Trees(凸包)

题意:有很多树,现在要用绳子包围起来,问最小的长度?

注意:(1)只有1个点的时候,输出0;

           (2) 如果所有点都在一条直线上,那么不需要包围起来。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define LL long long
#define M 1002
#define DEBUG puts("It's here!")
#define INF 1<<29
#define PI acos(-1.0)
#define CLS(x,v) memset(x,v,sizeof(x))
#define FOR(i,w,n)  for(int i=(w);i<=(n);++i)


struct point
{
    int x,y;
    void input()
    {
        scanf("%d%d",&x,&y);
    }
    point() {}
    point(int vx,int vy)
    {
        x=vx;
        y=vy;
    }
    point operator -(const point &b)
    {
        return  point(x-b.x,y-b.y);
    }
} p[M];
int cross(point a,point b )
{
    return a.x*b.y-a.y*b.x;
}
double dist(point a)
{
    return sqrt(1.0*a.x*a.x+a.y*a.y);
}
int cmp(point a,point b)
{
    int ret=cross(a-p[0],b-p[0]);
    if(ret==0)return dist(a-p[0])>dist(b-p[0]);
    else return ret>0;
}
int st[M],cnt;
int n,flag;
void graham()
{
    int k=0,i;
    for(i=1; i<n; i++)
        if(p[i].x<p[k].x||(p[i].x==p[k].x&&p[i].y<p[k].y))
        {
            k=i;
        }
    swap(p[0],p[k]);
    sort(p+1,p+n,cmp);
    cnt=0;i=2;
    st[cnt]=0;
    st[++cnt]=1;
    while(i<n)
    {
        while(cnt>1&&cross(p[st[cnt]]-p[st[cnt-1]],p[i]-p[st[cnt]])<0)
            cnt--;
        st[++cnt]=i++;
    }
    if(flag==0)st[++cnt]=0;//如果不在一条直线上就要圈回来
    double ans=0.0;
    for(i=1; i<=cnt; i++)
    {  // printf("-->%d %d\n",p[st[i]].x,p[st[i]].y);
        ans+=dist(p[st[i]]-p[st[i-1]]);
    }
    printf("%.2lf\n",ans);
}
bool allInline()
{
    for(int i=2;i<n;i++)
    if(cross(p[1]-p[0],p[i]-p[0]))
    {
        return 0;
    }
    return 1;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        flag=0;
        for(int i=0; i<n; i++)
        {
            p[i].input();
        }
        if(n<=1){printf("0.00\n");continue;}
        if(allInline())flag=1;//判断是否在一条线上
        graham();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值