POJ 1113 Wall

题目链接
凸包链接

Description

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King’s castle. The King was so greedy, that he would not listen to his Architect’s proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall.
在这里插入图片描述

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King’s requirements.

The task is somewhat simplified by the fact, that the King’s castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle’s vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King’s castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle.

Next N lines describe coordinates of castle’s vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King’s requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

Sample Input

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

Sample Output

1628

Hint

结果四舍五入就可以了

大致题意
给出一堆点,求这些点在凸包点的情况下,向外延伸一段距离的周长

具体思路
凸包的裸题,加上个延伸出去的一个圆的周长2Pil,答案四舍五入,记得不要直接取整,WA了好几发,要加上个0.5才算是四舍五入,在凸包栈的模拟中,因为要提去栈顶第一第二的元素,在这里建议模拟栈,不要直接stack,比较方便

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const double Pi=acos(-1.0);
const int max_pot=1e4+50;
int startx,starty;
struct Pot
{
    int x,y;
}pot[max_pot],zhan[max_pot];
bool cmp1(Pot x,Pot y)
{
    if (x.y==y.y)
    {
        return x.x<y.x;
    }
    return x.y<y.y;
}
bool cmp2(Pot x,Pot y)
{
    if (atan2(x.y-starty,x.x-startx)!=atan2(y.y-starty,y.x-startx))
    {
        return atan2(x.y-starty,x.x-startx)<atan2(y.y-starty,y.x-startx);
    }
    return x.x<y.x;
}
int judge(Pot a,Pot b,Pot c)
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dis(Pot a,Pot b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    int n,l,top=1;
    scanf("%d %d",&n,&l);
    for (int i=0;i<n;i++)
    {
        scanf("%d %d",&pot[i].x,&pot[i].y);
    }
    sort(pot,pot+n,cmp1);
    zhan[0]=pot[0];
    startx=zhan[0].x;
    starty=zhan[0].y;
    sort(pot+1,pot+n,cmp2);
    zhan[1]=pot[1];
    for (int i=2;i<n;i++)
    {
        while (judge(zhan[top-1],zhan[top],pot[i])<0)
        {
            top--;
        }
        top++;
        zhan[top]=pot[i];
    }
    double sum=0;
    for (int i=1;i<=top;i++)
    {
        sum+=dis(zhan[i],zhan[i-1]);
    }
    sum+=dis(zhan[top],zhan[0]);
    printf("%d\n",(int)(sum+2*Pi*l+0.5));
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值