题意:国王要建一座墙围住国王所有的城堡,要求墙上任意一点到城堡的距离大于L,同时墙的长度最短。输出墙的长度(四舍五入)。
墙的长度 = 城堡的点构成的凸包的长度 + 以L为半径的圆的周长
黑色的是城堡连接成的凸包,外围黄色的部分是墙,顶点上 是圆弧
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int Max = 1005;
const double PI = 3.141592653;
struct Point{
double x, y;
}p[Max];
int n, res[Max], top;
bool cmp(struct Point a, struct Point b){
if(a.y == b.y) return a.x < b.x;
return a.y < b.y;
}
double mult(Point sp, Point ep, Point op){
return (sp.x-op.x)*(ep.y-op.y) - (ep.x-op.x)*(sp.y-op.y);
}
double mydis(int i, int j){
double a = p[i].x - p[j].x;
double b = p[i].y - p[j].y;
return sqrt(a*a + b*b);
}
void Graham(){
int i, len;
top = 1;
sort(p, p+n, cmp);//p[0]是最左下的点,p[n-1]是最右上的点
for(i = 0; i < 3; i ++) res[i] = i;
for(i = 2; i < n; i ++){
while(top && mult(p[i], p[res[top]], p[res[top-1]]) >= 0) top --;
res[++ top] = i;
}//凸包画了下面一半
len = top;
res[++ top] = n-2;
for(i = n - 3; i >= 0; i --){
while(top != len && mult(p[i], p[res[top]], p[res[top-1]]) >= 0) top --;
res[++ top] = i;
}//剩下一半
}
int main(){
int i;
double r, ans;
scanf("%d%lf", &n, &r);
for(i = 0; i < n; i ++)
scanf("%lf%lf", &p[i].x, &p[i].y);
Graham();
ans = 2 * PI * r;
for(i = 0; i < top-1; i ++)
ans += mydis(res[i], res[i+1]);
ans += mydis(res[0], res[top-1]);
printf("%.lf\n", ans);
return 0;
}