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

提示:

结果四舍五入就可以了


题意解析:

题目大致意思为,国王命令用最少的石头和劳动力在整个城堡周围建造围墙,但要求围墙离城堡的距离不得超过一定距离,即 n 个点构成的城堡,给出每个点的坐标。若要修建距离城堡最近距离为 L 的城墙,问城墙的最短长度。

具体实现:

本题为经典的凸包模板题,在凸包的基础上加了一个半径为 L 的圆的周长 

用Graham扫描法求凸包

首先找到y轴的最低点,因为这个点一定在凸包上,故将其作为起始点,将其看作原点,将剩余的点相对于起始点的角度进行排序,从小到大遍历,当角度相同时,按照距离排序,离起始点近的排在前面,用一个栈存储凸包上的点,起始点和与起始点夹角最小的点一定在凸包上,先压入栈,然后开始遍历,将下一个点作为待定点入栈,将起始点与下一个点相连形成直线 L,如果待定点在 L 左边,则待定点确实为凸包上的点,否则将待定点出栈,继续找下一个点,直到最后一个点结束,最后栈中的点就是凸包上的所有点,将其两两之间的距离相加,再加上一个圆周长即为本题答案。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const double PI=4*atan(1.0);
const int N=1005;

struct Point{
    double x,y;
}p[N],q[N];

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 dis(Point a,Point b)//计算两点之间的距离 
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int cmp(Point a,Point b)//按极角排序 
{
    if(cross(p[0],a,b)>0)
        return 1;
    if(cross(p[0],b,a)==0 && (dis(p[0],b)>dis(p[0],a)))//共线情况下,距离近的优先 
        return 1;
    return 0;
}
int Graham(int n)
{
    int top=2;
    sort(p+1,p+n,cmp);
    q[0]=p[0];
    q[1]=p[1];
    q[2]=p[2];
    for(int i=3;i<n;i++)
	{
        while(top>=1 && cross(q[top-1],p[i],q[top])>=0)
        {
        	top--;
		}
        q[++top]=p[i];
    }
    return top;
}
int main()
{
    int n,l;
    scanf("%d %d",&n,&l);
    for (int i=0;i<n;i++)
    {
    	scanf("%lf %lf",&p[i].x,&p[i].y);
	}
    int minp=0;//找到最左下角的点,因为这个点一定在凸包上,所以将其作为起点 
    for (int i=0;i<n;i++)
	{
        if (p[minp].y>p[i].y)
        {
        	minp=i;
		}
		else if (p[minp].y==p[i].y)
		{
			if (p[minp].x>p[i].x)
			{
				minp=i;
			}
		}
    }
    swap(p[0],p[minp]);
    int G=Graham(n);
    double sum=2*PI*l+dis(q[0],q[G]);
    for(int i=1;i<=G;i++)
    {
    	sum+=dis(q[i],q[i-1]);
	}
    printf("%.0lf\n",sum);
    return 0;
}

总结:

凸包模板题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NsJhR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值