Wall
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
结果四舍五入就可以了
题目大意
给定 N 个点的坐标,在这些点所在区域外围上城墙,要求城墙与任意点的距离不小于 L ,求城墙的最小周长
分析
求凸包,在我们算出凸包的周长后再加上以 L 为半径的圆的大小即可(不难想),主要还是找凸包了,我在 b 站上学习了一下:[计算几何]Graham算法构造凸包(重制)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const double eps = 1e-6;
const double PI = acos(-1.0);
struct point {
double x, y;
}s[1010];
int sgn(double x) {//是否满足误差
if (fabs(x) < eps) return 0;
if (x < 0) return -1;
return 1;
}
double operator*(point a, point b) {//点乘
return a.x * b.x + a.y * b.y;
}
double operator^(point a, point b) {//叉乘
return a.x * b.y - a.y * b.x;
}
point operator-(point a, point b) {
point c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}
double dis(point a, point b) {//边长
return sqrt((a - b) * (a - b));
}
bool cmp(point a, point b) {//按照排序规则判断是否是凸包的边界点
double tmp = (a - s[0]) ^ (b - s[0]);
if (sgn(tmp) > 0 || (sgn(tmp) == 0 && dis(a, s[0]) < dis(b, s[0]))) return 1;
return 0;
}
int t[1010];
int top;
void Graham(int n) {
if (n == 1) {
top = 0;
t[0] = 0;
}
else if (n == 2) {
top = 1;
t[0] = 0;
t[1] = 1;
}
else if (n > 2) {
for (int i = 0;i <= 1;i++) t[i] = i;
top = 1;//凸包边界点数
for (int i = 2;i < n;i++) {
while (top > 0 && ((s[t[top]] - s[t[top - 1]]) ^ (s[i] - s[t[top - 1]])) <= 0) top--;
t[++top] = i;
}
}
}
int main() {
int n, r;
scanf("%d%d", &n, &r);
scanf("%lf%lf", &s[0].x, &s[0].y);
for (int i = 1;i < n;i++) {
scanf("%lf%lf", &s[i].x, &s[i].y);
if (s[i].y < s[0].y || s[i].y == s[0].y && s[i].x < s[0].x) swap(s[i], s[0]);//从小到上,从左到右遍历找凸包边界
}
sort(s + 1, s + n, cmp);
Graham(n);
double res = 0;
for (int i = 0;i < top;i++) res += dis(s[t[i]], s[t[i + 1]]);
res += dis(s[t[0]], s[t[top]]);//连成环
res += 2 * PI * (r * 1.0);
printf("%d\n", (int)(res + 0.5));
return 0;
}