Wall
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35666 | Accepted: 12168 |
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
Hint
结果四舍五入就可以了
这题题意是:
给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,并且墙与所有点的距离至少为L,求这个墙最小的长度。
例题中100为L的长度。
思路:我们思考后就认为这是一个凸包问题,我们就是把凸包顶点的边连起来求和再加上以L为半径的圆的周长即可,想想是不是这样。。。
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
struct node
{
int x, y;
}a[50005], b[50005];
int num;
int cross(node x, node y, node z)
{
return (y.x-x.x)*(z.y-x.y)-(z.x-x.x)*(y.y-x.y);
}
double dis(node x, node y)
{
return sqrt((double)(y.x-x.x)*(y.x-x.x)+(double)(y.y-x.y)*(y.y-x.y));
}
bool cmp( node p, node q )
{
/*p在q的顺时针方向*/
int temp = cross( a[0], p, q );
if ( temp > 0 || temp == 0&&dis(a[0], p) < dis(a[0], q) )
return 1;
return 0;
}
void graham(int n)
{
int i;
if ( n == 1 )
{
num = 1;
b[0] = a[0];
}
else if ( n == 2 )
{
num = 2;
b[0] = a[0], b[1] = a[1];
}
else
{
b[0] = a[0], b[1] = a[1];
num = 2;
for ( i = 2;i < n; i++ )
{
//保证b[num-1]-b[num-2]到a[i]-b[num-2]为逆时针才--
while ( num > 1 && cross (b[num-2], b[num-1], a[i]) <= 0 )
num--;
b[num++] = a[i];
}
}
}
int main()
{
int n, m, i, j;
while ( ~scanf ( "%d %d", &n, &m ))
{
int min1 = inf;
int h = 0;
for ( i = 0; i < n; i++ )
{
scanf ( "%d %d", &a[i].x ,&a[i].y );
if ( min1 == a[i].y )
{
if ( a[h].x > a[i].x )
{
h = i;
}
}
if ( min1 > a[i].y )
{
min1 = a[i].y;
h = i;
}
}
node temp = a[h];
a[h] = a[0];
a[0] = temp;
sort( a+1, a+n, cmp );
graham(n);
double sum = 0;
for ( i = 1;i < num ; i++ )
sum = sum+dis(b[i-1], b[i]);
sum = sum+dis(b[0], b[num-1])+2*PI*m;
printf ( "%d\n", (int)(sum+0.5) );
}
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!