传送门:POJ 1113
Problem Description
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
题目不难理解,就是求走一周的路程。
结果为:凸包周长 + 一个完整的圆周长。
因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=2e5+10;
const double eps=1e-8;
const double pi=acos(-1.0);
struct point
{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
};
typedef point Vector;
point lst[N];
int stk[N],top;
Vector operator - (point A,point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
int sgn(double x)
{
if(fabs(x)<eps)
return 0;
if(x<0)
return -1;
return 1;
}
double Cross(Vector v0,Vector v1)
{
return v0.x*v1.y-v1.x*v0.y;
}
double dis(point p1,point p2)
{
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)
{
int tmp=sgn(Cross(p1-lst[0],p2-lst[0]));
if(tmp>0)
return true;
if(tmp==0&&dis(lst[0],p1)<dis(lst[0],p2))
return true;
return false;
}
//点的编号 0 ~ n-1
//返回凸包结果stk[0 ~ top-1] 为凸包编号
void Graham(int n)
{
int k=0;
point p0;
p0.x=lst[0].x;
p0.y=lst[0].y;
for(int i=1;i<n;i++)
{
if(p0.y>lst[i].y||((p0.y==lst[i].y)&&(p0.x>lst[i].x)))
{
p0.x=lst[i].x;
p0.y=lst[i].y;
k=i;
}
}
lst[k]=lst[0];
lst[0]=p0;
sort(lst+1,lst+n,cmp);
if(n==1)
{
top=1;
stk[0]=0;
return;
}
if(n==2)
{
top==2;
stk[0]=0;
stk[1]=1;
return;
}
stk[0]=0;
stk[1]=1;
top=2;
for(int i=2;i<n;i++)
{
while(top>1&&Cross(lst[stk[top-1]]-lst[stk[top-2]],lst[i]-lst[stk[top-2]])<=0)
--top;
stk[top]=i;
++top;
}
return;
}
int n;
double l;
int main()
{
while(scanf("%d%lf",&n,&l)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&lst[i].x,&lst[i].y);
}
Graham(n);
double res=0;
for(int i=0;i<top-1;i++)
{
res+=dis(lst[stk[i]],lst[stk[i+1]]);
}
res+=dis(lst[0],lst[stk[top-1]]);
res+=2*pi*l;
printf("%d\n",int(res+0.5));
}
return 0;
}