杭电1348-Wall

来源:互联网 发布:win8电脑屏幕录制软件 编辑:程序博客网 时间:2024/04/30 06:43

Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2457    Accepted Submission(s): 678


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
19 100200 400300 400300 300400 300400 400500 400500 200350 200200 200
 

Sample Output
1628
第二道凸包问题
AC代码+解释:
#include<iostream>//凸包#include<string>#include<cstring>#include<cstdio>#include<algorithm>#include<iomanip>#include<cmath>#include<cstdlib>const int MAX=1001;const double PI=3.1415926;int n;int top;using namespace std;typedef struct Node{    double x;    double y;};Node s[MAX],stack[MAX];double Distance(Node a,Node b)//两点间的距离{    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double  Mul(Node p2, Node p1, Node p0)//向量叉积,我这里是逆时针来的,如果叉积大于0则符合{    return (p2.x - p0.x) * (p1.y - p0.y) - (p1.x - p0.x) * (p2.y - p0.y);}bool cmp(Node p2,Node p1)//快排{    double m;    m=Mul(p2,p1,s[0]);    if(m>0||(!m&&Distance(s[0],p2)<Distance(s[0],p1)))//如果叉积大于0按极角从小到大排序或者如果叉积等于0(即两向量共线),那么按距离偏短进行排序    return true;    return false;}void convex_hull()//凸包模板{    int i;    for(i=1;i<n;i++)    {        Node temp;        if(s[i].y<s[0].y||(s[i].y==s[0].y&&s[i].x<s[0].x))//找基点,即最开始的点        {            temp=s[0];            s[0]=s[i];            s[i]=temp;        }    }    sort(s+1,s+n,cmp);    top=1;    stack[0]=s[0];    stack[1]=s[1];    for(i=2;i<n;i++)    {        while(top>=1&&Mul(s[i],stack[top],stack[top-1])>=0)//凸包核心        top--;        top++;        stack[top]=s[i];    }}int main(){    int i,p=0,t,l;    double sum;    while(cin>>t)    {        while(t--)        {            cin>>n>>l;            cout.setf(ios::fixed);            cout.precision(0);            for(i=0;i<n;i++)            cin>>s[i].x>>s[i].y;            convex_hull();            sum=0;            for(i=0;i<top;i++)            {                sum+=Distance(stack[i],stack[i+1]);            }            sum+=Distance(stack[0],stack[top]);            sum+=2*double(l)*PI;//除了平行城堡l的长度还有加上4个4分之一半径为l圆弧即半径为l的元的周长            cout<<sum<<endl;            if(t)            cout<<endl;        }    }    return 0;}


原创粉丝点击