Wall

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 21731

 

Accepted: 7132

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

结果四舍五入就可以了

Source

Northeastern Europe 2001

 解题报告:题意就是修一圈的城墙,把凸包围起来,并且城墙离凸包的距离不小于R,求最小需要多长;就是求凸包的周长 + 圆的周长即可!

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 1010;
const double PI = acos(-1.0);
double r, ans;
int n, top;
struct Point//存储点的坐标
{
    double x;
    double y;
}p[MAX];
int convex[MAX];//记录形成凸包的点的次序按逆时针
double cmp(struct Point a, struct Point b)//先按坐标y排序y坐标相同时再按x排序
{
    if (a.y == b.y) return a.x < b.x;
    else return a.y < b.y;
}
double Dis(int i, int j)//计算两点间的距离
{
    return sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y));
}
double Multi(Point p1, Point p2, Point p3)
{
    return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}
void Graham()//选取点形成凸包(不包含边上的点)选取的点数是最小的
{
    int i, count;
    top = 1;
    sort(p, p + n, cmp);
    for (i = 0; i < 3; ++i)
    {
        convex[i] = i;
    }
    for (i = 2; i < n; ++i)
    {
        while (top && Multi(p[i], p[convex[top]], p[convex[top - 1]]) >= 0)
        {
            top --;
        }
        convex[++ top] = i;
    }
    count = top;
    convex[++ top] = n - 2;
    for (i = n - 3; i >= 0; i --)
    {
        while (top != count && Multi(p[i], p[convex[top]], p[convex[top - 1]]) >= 0)
        {
            top --;
        }
        convex[++ top] = i;
    }
}
int main()
{
    int i;
    while (scanf("%d%lf", &n, &r) != EOF)
    {
        for (i = 0; i < n; ++i)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        ans = 2.0 *PI * r;
        Graham();
        for (i = 0; i < top; ++i)
        {
            ans += Dis(convex[i], convex[(i + 1) % top]);
        }
        printf("%.0lf\n", ans);
    }
    return 0;
}
posted on 2012-05-06 16:12  Stephen Li  阅读(279)  评论(0编辑  收藏  举报