题目:
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.
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
结果四舍五入就可以了
题目:
国王要给皇宫建围墙,皇宫内有一些建筑物,这些围墙要距离这些建筑物至少得L的距离,问你围墙的长度最短是多长。
思路:
建筑物凸包周长+半径为L的圆形的周长
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1005;
const double eps = 1e-8;
const double PI = acos(-1.0);
//对于浮点数的,><=0的判断。
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);
}
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;
}
//绕原点旋转角度B(弧度制)后,x,y的变化。
void transXY(double B)
{
double tx = x, ty = y;
x = tx*cos(B)-ty*sin(B);
y = tx*sin(B)+ty*cos(B);
}
};
double dist(Point a, Point b)
{
//勾股定理,使用点积
return sqrt((a-b)*(a-b));
}
Point p[maxn];
int n;
double l;
//做极角排序
bool _cmp(Point p1,Point p2)
{
double tmp = (p1-p[1])^(p2-p[1]); //向量叉乘
if(sgn(tmp)>0) return true;
else if(sgn(tmp)==0&&sgn(dist(p1,p[1])-dist(p2,p[1]))<=0)
return true;
else return false;
}
int Stack[maxn],top;
void Graham()
{
Point p0;
int k = 1;
p0 = p[1];
//找最下方的一个点
for(int i = 2; i <= n; i++)
{
if(p0.y>p[i].y||(p0.y==p[i].y&&p0.x>p[i].x))
{
p0 = p[i];
k = i;
}
}
swap(p[1],p[k]);
sort(p+2,p+n+1,_cmp);
if(n == 1)
{
top = 1;
Stack[0] = 1;
return ;
}
if(n == 2)
{
top = 2;
Stack[0] = 1;
Stack[1] = 2;
return ;
}
Stack[0] = 1;
Stack[1] = 2;
top = 2;
for(int i = 3; i <= n; i++)
{
while(top>1&&sgn((p[Stack[top-1]]-p[Stack[top-2]])^(p[i]-p[Stack[top-2]]))<=0) top--;
Stack[top++] = i;
}
}
int main()
{
scanf("%d%lf", &n, &l);
for(int i = 1; i <= n; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
}
Graham();
double ans = dist(p[Stack[0]],p[Stack[top-1]]);
for(int i = 1; i < top; i++)
{
ans += dist(p[Stack[i]],p[Stack[i-1]]);
}
ans+= 2*PI*l;
printf("%d\n", (int)(ans+0.5));
return 0;
}