Wall

Wall

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 164 Accepted Submission(s): 63
 
Problem 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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 
Sample Input
1

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
 
Sample Output
1628
 
 
Source
Northeastern Europe 2001
 
Recommend
JGShining
 
/*
题意:给你零散的建筑的坐标,让你建围墙,建筑物距离围墙的距离不能小于L,输出围墙最小的周长

初步思路:求出凸包,然后在凸包外面“裹”上一层城墙,实际上就是凸包的周长,加上以L为半径的圆
    周长,这里很好证明的:因为每个拐点都是一个拐角内角度的弧长,总和就是凸包内角和长的的弧
    长,而凸多边形的内角和是π,所以就是相当于加上了,一个以L为半径的圆的周长
    
1Y纪念一下,哈哈哈哈!
*/
#include<bits/stdc++.h>
#define pi acos(-1)
using namespace std;
/****************************凸包模板*******************************/
const double eps = 1e-8;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉积
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //点积
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    void input(){
        scanf("%lf%lf",&x,&y);
    }
};
struct Line { 
    Point s,e; 
    Line(){} 
    Line(Point _s,Point _e) {         
        s = _s; e = _e; 
    }
}; 
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}   
/*
* 求凸包,Graham算法
* 点的编号0~n-1
* 返回凸包结果Stack[0~top-1]为凸包的编号
*/
const int MAXN = 1010;
Point List[MAXN];
int Stack[MAXN];//用来存放凸包的点
int top;//表示凸包中点的个数
//相对于List[0]的极角排序
bool _cmp(Point p1,Point p2)
{
    double tmp = (p1-List[0])^(p2-List[0]);
    if(sgn(tmp) > 0)
        return true;
    else if(sgn(tmp) == 0 && sgn(dist(p1,List[0]) - dist(p2,List[0])) <= 0)
        return true;
    else 
        return false;
}
void Graham(int n)
{
    Point p0;
    int k = 0;
    p0 = List[0];
    //找最下边的一个点
    for(int i = 1;i < n;i++)
    {
        if( (p0.y > List[i].y) || (p0.y == List[i].y && p0.x > List[i].x) )
        {
            p0 = List[i];
            k = i;
        }
    }
    swap(List[k],List[0]);
    sort(List+1,List+n,_cmp);
    if(n == 1)
    {
        top = 1;
        Stack[0] = 0;
        return;
    }
    if(n == 2)
    {
        top = 2;
        Stack[0] = 0;
        Stack[1] = 1;
        return ;
    }
    Stack[0] = 0;
    Stack[1] = 1;
    top = 2;
    for(int i = 2;i < n;i++)
    {
        while(top > 1 && sgn((List[Stack[top-1]]-List[Stack[top-2]])^(List[i]-List[Stack[top-2]])) <= 0)
            top--;
        Stack[top++] = i;
    }
}
/****************************凸包模板*******************************/
int t,n;
double l;
int main(){
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    for(int ca=0;ca<t;ca++){
        if(ca)
            printf("
");
        scanf("%d%lf",&n,&l);
        for(int i=0;i<n;i++){
            List[i].input();
        }//输入边
        if(n==1){
            printf("%.0lf
",2*pi*l);
            continue;
        }
        if(n==2){
            printf("%.0lf
",2*pi*l+2*dist(List[0],List[1]));
            continue;
        }
        Graham(n);
        double cur=0;
        for(int i=0;i<top;i++){
            cur+=dist(List[Stack[i%top]],List[Stack[(i+1)%top]]);
        }
        printf("%.0lf
",2*pi*l+cur);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6231012.html