POJ - 1113 Wall

Wall
Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

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. 
POJ - 1113 Wall_第1张图片
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  

#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<iomanip>
using namespace std;
#define mx 1005
#define mxv 100000
#define pi acos(-1.0)
struct point
{
    int x,y;
}pt[mx];
int cmp(point,point);
bool IsLeftTurn(point,point ,point);
int cross(point,point,point);
double dis(point,point);
int main()
{
    int n,len;
    scanf("%d%d",&n,&len);
    int i,k,j;
    point  p0;
    p0.x=mxv;p0.y=mxv;
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&pt[i].x,&pt[i].y);
        if(p0.y>pt[i].y)
        {
            p0=pt[i];
            k=i;
        }
        else if(p0.y==pt[i].y&&p0.x>pt[i].x)
        {
            p0.x=pt[i].x;
            k=i;
        }
    }
    pt[k]=pt[0];
    pt[0]=p0;
    sort(pt+1,pt+n,cmp);
    int t=1;
    for(i=2;i<n;i++)
        if(cross(pt[i],pt[t],pt[0])==0)
    {
        pt[i].x=mxv;
        pt[i].y=mxv;
    }
    else t=i;
    int stack[mx],top=-1;
    stack[++top]=0;
    i=1;
    while(pt[i].x==mxv)i++;
    stack[++top]=i;
    while(pt[i].x==mxv)i++;
    stack[++top]=i;
    for(i++;i<n;i++)
    {
        if(pt[i].x!=mxv)
        {
            while(!IsLeftTurn(pt[i],pt[stack[top]],pt[stack[top-1]]))
                top--;
            stack[++top]=i;
        }
    }
    double minlen=0;
    for(i=0;i<top;i++)
        minlen+=dis(pt[stack[i]],pt[stack[i+1]]);
    minlen+=dis(pt[stack[i]],pt[stack[0]]);
    minlen+=2.0*pi*len;
    cout<<setiosflags(ios::fixed)<<setprecision(0)<<minlen<<endl;
}
int cmp( point p,point q)
{
    if(cross(p,q,pt[0])<0)return 0;
    if(cross(p,q,pt[0])>0)return 1;
    if(dis(p,pt[0])>dis(q,pt[0]))
        return 1;
    return 0;
}
bool IsLeftTurn(point p,point q,point r)
{
    if(cross(p,q,r)<0)return true;
    return false;
}
int cross(const point p,point q,point r)
{
    return (p.x-r.x)*(q.y-r.y)-(p.y-r.y)*(q.x-r.x);
}
double dis( point p, point q) //计算相邻两点之间的距离
{
return sqrt(double((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y)));
}


你可能感兴趣的:(Algorithm,ACM,poj,uva,现场赛)