hdu 1392(凸包)

传送门
题意:几乎是模板题,求凸包的周长

用Graham扫描法解决(还有一个叫jarvis步行法,目测也差不多)。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=102;
struct NODE {
    int x,y;
}a[maxn],s[maxn];
int top,n;
inline int read() {
    int x=0;char c=getchar();
    while (c<'0'||c>'9') c=getchar();
    while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x;
}
inline double dis(NODE a,NODE b) {
    return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline int cross(NODE a,NODE b,NODE c) {
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
inline int cmp1(NODE a,NODE b) {
    return a.y<b.y||(a.y==b.y&&a.x<b.x);
}
inline int cmp2(NODE c,NODE d) {
    int temp=cross(a[0],c,d);
    if (temp==0) return dis(a[0],c)<dis(a[0],d);//three points collinear
    return temp>0;
}
int main() {
//  freopen("hdu 1392.in","r",stdin);
    while (n=read()) {
        for (int i=0;i<n;++i) a[i].x=read(),a[i].y=read();
        if (n==1||n==2) {printf("%.2lf\n",n^1?dis(a[0],a[1]):0.0);continue;}//special judge
        sort(a,a+n,cmp1),sort(a+1,a+n,cmp2),
        top=-1,
        s[++top]=a[0],s[++top]=a[1],s[++top]=a[2];
        for (int i=3;i<n;++i) {
            while (top&&cross(s[top-1],s[top],a[i])<=0) --top;
            //strictly '<=', if three points are collinear, choose the first ans the last 
            s[++top]=a[i];
        }
        double c=0.0;
        for (int i=1;i<=top;++i) c+=dis(s[i-1],s[i]);
        c+=dis(s[top],s[0]);
        printf("%.2lf\n",c);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值