http://poj.org/problem?id=1113 (题目链接)
题意
给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,并且墙与所有点的距离至少为L,求这个墙最小的长度。
Solution
凸包裸题。凸包的构造的话,有一篇博客写得很好,就是看着有点乱,主题太丑了= =。
很容易发现,所求的的墙的最小长度实际上就是平面凸包的周长加上以L为半径的圆的周长。这个圆是怎么来的呢,其实很好理解。对于城堡的顶点到墙的距离,想要墙尽可能短,那么一定是以顶点为圆心,L为半径的圆弧。
比如说这个,城墙的4个角就是各为90°的圆弧,很容易脑补出对于任意一个凸包,圆弧的总度数相加一定是360°。
但是这里有一个问题困扰了我很久,为什么一定要删去凸包上共线的点,不删去的话好像也没有什么影响,可是交上去就Wa。拍了下,发现原来当两个点重复的时候就Gi了= =,至于为什么,自己画个图好好想想吧。
提供一组有重复点的数据:
7 0
9 0
5 1
3 3
5 4
5 4
7 6
9 519
代码
// poj1113
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<map>
#define inf 2147483640
#define LL long long
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=1010;
struct point {int x,y;}p[maxn];
int n,l,top,s[maxn]; int Cross(point p0,point p1,point p2) { //叉乘
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double Dis(point a,point b) {
return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(point a,point b) {
int t=Cross(p[1],a,b);
if (t>0) return 1;
else if (t==0 && Dis(p[0],a)<Dis(p[0],b)) return 1;
else return 0;
}
void Graham() {
if (n==1) {top=1;s[1]=1;}
else if (n==2) {top=2;s[1]=1;s[2]=2;}
else {
s[1]=1;s[2]=2;
top=2;
for (int i=3;i<=n;i++) {
while (top>1 && Cross(p[s[top-1]],p[s[top]],p[i])<=0) top--;
s[++top]=i;
}
}
}
int main() {
while (scanf("%d%d",&n,&l)!=EOF) {
int k=1;
for (int i=1;i<=n;i++) {
scanf("%d%d",&p[i].x,&p[i].y);
if (p[i].x<p[k].x || (p[i].x==p[k].x && p[i].y<p[k].y))
k=i;
}
point p0=p[k];
p[k]=p[1];p[1]=p0;
sort(p+2,p+1+n,cmp);
Graham();
double ans=0;
for (int i=1;i<top;i++) ans+=Dis(p[s[i]],p[s[i+1]]);
ans+=Dis(p[s[1]],p[s[top]]);
ans+=2*Pi*l;
printf("%d\n",(int)(ans+0.5));
}
return 0;
}