题目:
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.
输入:
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.
输出:
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.
样例输入:
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
样例输出:
1628
提示:结果四舍五入就可以了
题意描述:
有个国王非常麻烦,为了尽量减少使用的材料(真抠啊),要使得围城的城墙的周长最小,并且还和城堡保持L的距离,把城堡上所有的点往外移L的距离,求能包裹所有点的最小凸多边形,凸包;输出最小周长,答案四舍五入。
由于在外移过程中,会存在弧度,可以把这些弧度从他的中心点进行切割,就会惊奇的发现就是城堡的周长(当然要去掉内凹的部分周长会在算法执行的时候省略),加上以外移距离为半径的圆。
思路:凸包
使用求凸包的andrew算法,先对所有的点按x为第一关键字升序排序
先求下凸包(下凸包就是能包裹下面所有点的凸边形),先把x最小的放进栈里,然后以x坐标从小到大遍历所有点,由于是求下凸包,所以我们要取的点尽可能在下面,比较栈顶的两个元素,如果该点位于栈顶两个元素构成的直线的下面,说明取这个点是更优的,更可以把所有点包含在下凸包里,所以我们要把栈里这样的点逐步弹出,直到栈里只剩下一个,或者该点位于栈顶两个元素构成的直线上面那么就不需要弹出了,把该点放入;
上凸包求法和下凸包类似,但是是从x从大到小进行遍历,因为是上凸包,我们想要的点应该尽可能在上面,所以当该点位于栈顶两个元素构成的直线的上面,就把栈里的元素逐步弹出,直到栈里元素只剩下刚刚求的下凸包栈里元素数量加一或者位于该点位于两直线之下就停止;
怎么判断该点位于某条直线的上面还是下面呢?
我们非常容易想到叉积(简单的右手法则判断方向),因为这里都是逆时针,所以只要叉积是负的这个点就是位于外部的;
常规思路:暴力枚举? 反正我是枚举不出来
通过代码:
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define x first
#define y second
#define PI 3.1415926535897
using namespace std;
typedef long long ll;
const int N=1010;
int n,r,top;
typedef struct node
{
int x,y;
}point;
point p[N],st[N*4];
bool cmp(point a,point b)
{
return a.x<b.x;
}
int check(point a,point b,point c)
{
point a1,a2;
a1.x=a.x-b.x,a1.y=a.y-b.y;
a2.x=c.x-b.x,a2.y=c.y-b.y;
return a1.x*a2.y-a1.y*a2.x;
}
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double andrew()
{
//求下凸包;
for(int i=0;i<n;i++)
{
while(top>1&&check(st[top],st[top-1],p[i])<=0) top--;
st[++top]=p[i];
}
int t=top;
//求下凸包由于最后一个点一定位于在凸包上,所以直接从n-1开始枚举;
for(int i=n-2;i>=0;i--)
{
while(top>t&&check(st[top],st[top-1],p[i])<=0) top--;//倒数第二个一定会进来,因为top>t第一次一定是top==t
st[++top]=p[i];
}
double res=0;
//x最小的那个一定会进来两次,所以只要枚举到top-1;
for(int i=1;i<top;i++)
{
res+=dis(st[i],st[i+1]);
}
return res;
}
void solve()
{
cin>>n>>r;
for(int i=0;i<n;i++)
{
cin>>p[i].x>>p[i].y;
}
sort(p,p+n,cmp);//将每个点按x从小到大排序;
if(n==2)
{
double res=r*2*PI+dis(p[0],p[1])*2;
printf("%.0lf\n",res);
return;
}
double res=andrew();
res+=2*PI*r;
//然后四舍五入;
//cout<<res<<endl;
printf("%.0lf\n",res);
}
int main()
{
//IOS;
int T;
T=1;
while(T--)
{
solve();
}
}