S - Wall POJ - 1113 (凸包)

S - Wall POJ - 1113  

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

结果四舍五入就可以了

 包围城堡的最少的围墙=形成的凸包的周长+以L为半径的圆周的周长

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1000010;
const int nmax = 51010;
const double esp = 1e-8;
const double PI=acos(-1.0);
int n,minl;
struct point
{
    int x,y;
    point() {}
    point(int _x,int _y):x(_x),y(_y)
    {
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    int operator ^(const point &b)const
    {
        return x*b.y-y*b.x;
    }
    bool operator <(const point &b)const
    {
        if(y==b.y)
            return x<b.x;
        return y<b.y;
    }
} a[nmax],b[nmax],p0; //数组a保存原来的点,数组b保存凸包上的点
double dis(point p1,point p2)
{
    point p=p1-p2;
    double ans=(double)p.x*p.x+p.y*p.y;
    return sqrt(ans);
}
bool cmp(point p1,point p2)
{
    int k=(p1-p0)^(p2-p0);
    if(k==0)
    {
        return dis(p1,p0)<dis(p2,p0);  //幅度相等则距离第一个点P0近的在前面
    }
    else if(k>0)  //幅度小的在前面
        return true;
    else
        return false;
}
int top=1;
void graham()  //求凸包模板
{
    int k=0;
    p0=a[0];
    for(int i=1; i<n; i++)
    {
        if(a[i].y<p0.y||(a[i].y==p0.y&&a[i].x<p0.x))
        {
            k=i;
            p0=a[i];  //找第一个点p0
        }
    }
    a[k]=a[0];
    a[0]=p0;
    sort(a+1,a+n,cmp);
    b[0]=a[0];
    b[1]=a[1];
    for(int i=2; i<n; i++)
    {
        while(top>0&&((b[top]-b[top-1])^(a[i]-b[top-1]))<=0)
            top-- ; 
  /*扫描到的凸包的当前点b[top]与上一个凸包的点b[top-1]的连线要与遍历的点a[i]与凸包的上一个点b[top-1]的连线成逆时针方向
*/
        b[++top]=a[i];
    }
}
void solve()
{
    b[++top]=b[0];
    double sum=0;
    for(int i=0; i<top; i++)
    {

        sum+=dis(b[i],b[i+1]);
    }
    sum+=PI*2*minl;
    printf("%d\n",int(sum+0.5));//四舍五入
}
int main()
{
    while(scanf("%d%d",&n,&minl)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        graham();
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/82958132