Link: poj 1113
Given n vertices of a polygon Castle, a wall is built around the outside of the castle to enclose all vertices,
And the distance between the wall and all vertices is at least l. Find the minimum length of the wall.
Idea: minimum length = the total side length of the convex hull formed by the castle vertex + the circle perimeter with a radius of L
Use the Graham algorithm to obtain the convex hull, and then enumerate its vertices to find the edge length between the two. Remember to add the edge length of the first vertex and the last vertex.
The rounded integer result can be output using double storage and %. 0lf output.
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct stu{ int x,y;}p[1010],s[1010];int m,top;int chaji(struct stu p1,struct stu p2,struct stu p3){ return (p1.x-p2.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p1.y-p2.y);}double dis(struct stu p1,struct stu p2){ return sqrt((p1.x-p2.x)*(p1.x-p2.x)*1.0+(p1.y-p2.y)*(p1.y-p2.y));}int cmp(struct stu p1,struct stu p2){ int k; k=chaji(p1,p[0],p2); if(k>0||(k==0&&dis(p1,p[0])<dis(p2,p[0]))) return 1; return 0;}void graham(){ struct stu t; int k=0,i; for(i=1;i<m;i++) if(p[i].y<p[k].y||(p[i].y==p[k].y&&p[i].x<p[k].x)) k=i; t=p[k]; p[k]=p[0]; p[0]=t; sort(p+1,p+m,cmp); s[0]=p[0]; s[1]=p[1]; top=1; for(i=2;i<m;i++){ while(top>=1&&chaji(s[top-1],s[top],p[i])>=0) top--; top++; s[top]=p[i]; }}int main(){ int n,i; double sum; while(scanf("%d%d",&m,&n)!=EOF){ for(i=0;i<m;i++) scanf("%d%d",&p[i].x,&p[i].y); graham(); sum=dis(s[0],s[top])+2*3.1415926*n; for(i=1;i<=top;i++) sum+=dis(s[i-1],s[i]); printf("%.0lf\n",sum); } return 0;}