HDU 1348 && POJ 1113 Wall(凸包)

题目链接:

HDU :http://acm.hdu.edu.cn/showproblem.php?pid=1348
POJ :http://poj.org/problem?id=1113

题解:

求出凸包后,算出凸包的周长,再加上以l为半径的圆的周长就好了,最后输出要一个整数并且四舍五入,HDU上还会卡一手输出,最后一个t不要再输出一行空格。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 1050;
const double PI = acos(-1.0);

struct node
{
    int x, y;
}e[maxn], tubao[maxn];

int cmp(const node& a , const node& b)
{
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}

int cross(node a, node b, node c)
{
    return (a.x - b.x) * (c.y - b.y) - (a.y - b.y) * (c.x - b.x);
}

int convex(int n)
{
    sort(e, e+n, cmp);
    int m = 0;
    for(int i = 0; i < n; i++)
    {
        while(m > 1 && cross(tubao[m-1], tubao[m-2], e[i]) <= 0)
            m--;
        tubao[m++] = e[i];
    }
    int k = m;
    for(int i = n-2; i >= 0; i--)
    {
        while(m > k && cross(tubao[m-1], tubao[m-2], e[i]) <= 0)
            m--;
        tubao[m++] = e[i];
    }
    if(n > 1)m--;
    return m;
}

double dis(const node &a, const node &b)//一定要double
{
    return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int main()
{
    int t,m,l;
    cin >> t;
    while(t--)
    {
        scanf("%d%d", &m, &l);
        for(int i = 0; i < m; i++)
            scanf("%d%d", &e[i].x, &e[i].y);
        int num = convex(m);
        double ans = 0;
        for(int i = 1; i < num; i++)
        {
            ans += dis(tubao[i], tubao[i-1]);
        }
        ans += dis(tubao[num-1], tubao[0]);
        ans += PI * l * 2;
        printf("%.0f\n", ans);//这样写才不会WA
        if(t)
            cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值