TZOJ 1471 Wall 凸包模板题

 

描述:

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

题意:故事就不翻译了因为我看不懂,题目大意有一个多边形建筑,有人要你建造围墙环绕建筑,但那人要求有点多,他又要减cost又要你不能触碰到建筑的l米内,你要算的是围墙最少要多长;

第一眼看,英文看不懂,第二眼看,好怪再看一眼,第三眼看,感觉把几个点围出的圈的长度算出来就行,但有圆弧要怎么求,最后通过一个同类型的题发现根本不是这么单纯的求长度,且这种有在点lm外的条件的题在答案后统一长度加个圆的周长就行;

总之,这是个凸包模板题;

凸包是在平面上能包含所有给定点的最小凸多边形;

而求凸包有三步骤:

1.将所有存储的点进行从低到高的排序(x先y先看你心情);

2.求出上凸包和下凸包:

用一个栈来存储你记录下的点,将每个点进行叉积判定,定旧入点为a(三个点形成的角顶点的位置),新入点为b,你要判定的点c,当叉积小于0,则ac在ab内侧(或叉积等于0,三点共线),则b需要被删除,最后再加入判定点c于栈顶;(其实说这么麻烦你只要保证两次的循环都是同一个判定条件即可,一个为顺时针则另一个必为求逆时针,实在不行就把排序反一反,总能对(咳咳))

1-n遍历后再遍历n-1就能通过两次的求解得到留下的点,这些点是我们要求长度的点;

3.求出两点之间距离进行累加;

注意:此题有个额外要求离点距离至少为l,所以最后求出的总和需要再加上一个圆周(2pi*l)的长度,最后不要忘了四舍五入;

以上便可求出最小长度;

以下为代码

#include<bits/stdc++.h>
using namespace std;
const double pi=acos(-1.0);
struct point
{
	int x;
	int y;
}dian[1005],s[5005];
int n,l,top=0; 
int cmp(point a,point b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y<b.y;
}
double cross(point a,point b,point c)
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dist(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double andrew()
{
	sort(dian,dian+n,cmp);
	for(int i=0;i<n;i++)
	{
		while(top>1&&cross(s[top-1],s[top],dian[i])<=0)top--;
		s[++top]=dian[i];
	}
	int t=top;
	for(int i=n-1;i>=0;i--)
	{
		while(top>t&&cross(s[top-1],s[top],dian[i])<=0)top--;
		s[++top]=dian[i];
	}
	double res=pi*l*2;
	for(int i=1;i<top;i++)
	{
		res=res+dist(s[i],s[i+1]);
	}
	return res;
}
int main()
{
	ios::sync_with_stdio(false);
    cin>>n>>l;
    for(int i=0;i<n;i++)
    {
    	cin>>dian[i].x>>dian[i].y;
	}
	int ans=int(andrew()+0.5);
	cout<<ans<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值