Wall
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29118 | Accepted: 9745 |
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.
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.
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
题意:求得n个点的凸包,然后求与凸包相距 l 的外圈的周长。
所求周长=凸包周长+半径为l的圆周长
#include<stdio.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
#define Max 1003
#define PI 3.141592653
int top;
struct point
{
double x,y;
}p[Max],stack[Max];
double dis(point p1,point p2) // 两点的距离的平方
{
return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
double multi(point p1,point p2,point p0) //叉积
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
int cmp(point a,point b)
{
if(multi(a,b,p[0])>0) return 1;
if(multi(a,b,p[0])==0 && dis(a,p[0])<dis(b,p[0])) return 1;
return 0;
}
void GS(point p[],point stack[],int n) //Graham_scan算法
{
int i,k=0;
top=2;
point temp;
for(i=0;i<n;i++) //寻找最下且偏左的点
if(p[i].y<p[k].y||((p[i].y)==p[k].y)&&(p[i].x<p[k].x))
k=i;
temp=p[0]; //将最下且偏左的点指定为p[0]
p[0]=p[k];
p[k]=temp;
sort(p+1,p+n,cmp); //按极角从小到大,距离偏短进行排序
stack[0]=p[0];
stack[1]=p[1];
stack[2]=p[2];
for(i=3;i<n;i++)
{
while(top>1 && multi(p[i],stack[top],stack[top-1])>=0)
top--; // 出栈
stack[++top]=p[i]; // 进栈
}
}
int main ()
{
double sum;
int t,n,l,i,j;
while(~scanf("%d",&t))
{
scanf("%d%d",&n,&l);
sum=0;
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
GS(p,stack,n); // n个点
for(i=0;i<top;i++)
sum+=sqrt(dis(stack[i],stack[i+1])); // 凸包每条边的长度
sum+=sqrt(dis(stack[top],stack[0])); // 注意 首尾两点连接成边 的长度
printf("%.0lf\n",sum+2*PI*l); // 凸包周长加外圈的周长 为所求
}
return 0;
}