HDU 1392 Surround the Trees

Surround the Trees - http://acm.hdu.edu.cn/showproblem.php?pid=1392

代码

// hdu 1392 Surround the Tress
#include<bits/stdc++.h>
#define LL long long
using namespace std;
#define MXN 110
struct Point{
    LL x, y;
        // 与点p是否是为同一个点
    bool operator==(const Point &p){ 
        return x == p.x && y == p.y;
    }
    // 1:y最小,2:y相等时x最小
    bool operator<(const Point &p){
        if(y == p.y) return x < p.x;
        return y < p.y;
    }
    // 点p(x,y)与点p1和p2构成的向量p->p1、p->p2的叉积
    LL cross(const Point &p1, const Point &p2){
        return (p1.x - x)*(p2.y - y) - (p1.y - y)*(p2.x - x);
    }
    double distance(const Point &p){
        return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
    }
};
int main(){ 
	int n, q[MXN];
    Point tr[MXN];
    while(scanf("%d", &n) && n){               
        Point p = {32768, 32768};
        for(int i = 0; i < n; i++){
            scanf("%lld %lld", &tr[i].x, &tr[i].y);
            if(tr[i] < p) p = tr[i];
        }
        if(n == 1){
            printf("%.2f\n", 0);
            continue;
        }
        else if(n == 2){
            printf("%.2f\n", tr[0].distance(tr[1])); // 不明白为什么不是算2倍?
            continue;
        }
        sort(tr, tr+n, [&p](Point &p1, Point &p2){
            if(p1 == p || p2 == p) return p1 == p;
            LL res = p.cross(p1, p2);
            if(res) return res > 0;
            return p1.y < p2.y;
        });
        int k = 1;
        q[0] = 0, q[1] = 1;
        for(int i = 2; i <= n; i++){
            while(tr[q[k-1]].cross(tr[q[k]], tr[(i)%n]) < 0) k--;
            q[++k] = i == n ? 0 : i;
        }
        double dis = 0;
        for(int i = 1; i <= k; i++) dis += tr[q[i-1]].distance(tr[q[i]]);
        printf("%.2f\n", dis);
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jpphy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值