[Northeastern Europe 2001] Wall(凸包)

文章讲述了贪婪的国王要求建筑师用最少资源建造围墙,且围墙必须远离城堡。问题转化为求最小凸包的周长加上半径为特定距离的圆周长。解题方法涉及Graham扫描算法。
摘要由CSDN通过智能技术生成

【题目描述】

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

提示:结果四舍五入就可以了

解题思路:

这道题是道凸包模板题,这里可以用Graham扫描法处理,题中所求围墙的最小长度可等价与城堡对应的最小凸包的周长加上一个半径为l的圆的周长。因为围墙与城堡的距离最小值为l,则凸包的每个顶点与围墙的距离为l,那么所求围墙的周长可理解为由凸包各边之和+任意两相邻边之间的圆弧长之和,而这些圆弧其实能够组成一个半径为l的圆。

#include<bits/stdc++.h>
#define P acos(-1.0)
const int N=1e3+5;
using namespace std;
int n,l,t;
struct node{
	int x,y;
}a[N];
stack<node> s;
int sta[N];

int cross(node p0,node p1,node p2) //计算叉积
{
   return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
} 
 
double count(node p1,node p2) //计算p1-p2的距离 
{
    return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
 
bool cmp(node x,node y) //极角相同则距离远的靠后 
{
	int d=cross(a[0],x,y);
	if(d>0 || (!d && count(a[0],x)<count(a[0],y))) return 1;
	return 0;
}

int main()
{
	while(cin>>n>>l)
	{
		node p0;
		double ans=2.0*P*l;
		int k=0;
		cin>>a[0].x>>a[0].y;
		p0.x=a[0].x;
		p0.y=a[0].y;
		for(int i=1;i<n;i++)
		{
			cin>>a[i].x>>a[i].y;
			if((p0.y==a[i].y && p0.x>a[i].x) || p0.y>a[i].y)
			{
				p0.x=a[i].x;
				p0.y=a[i].y;
				k=i;
			}	
		}
		a[k]=a[0];
		a[0]=p0;
		sort(a+1,a+n,cmp);
		for(int i=0;i<=1;i++)
			sta[i]=i;
		t=1;
		for(int i=2;i<n;i++)
		{
			while(t && cross(a[sta[t-1]],a[sta[t]],a[i])<=0) t--;
			sta[++t]=i;
		}
		for(int i=0;i<t;i++)//求和 
			ans+=count(a[sta[i]],a[sta[i+1]]);
		ans+=count(a[sta[0]],a[sta[t]]);
		printf("%.0f\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值