`

PKU 1113 Wall .

阅读更多
Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18398   Accepted: 5992

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

Hint

结果四舍五入就可以了

Source

 
琴日我地EK1412分享左滴题目,其中101majia就分比我地呢个凸包入门题目。
 
之后今日早上用左个几钟睇完导论里面几何部分,发觉自己以前果中高中计算方法真系落后鸟{=      =}。
用高数里面既叉积实在可以做到好多野,而且精度高,判断又快,基本可以称得上系几何算法既核心。
关于叉积(CrossProduct)
大家可以系高数或者其他几何书度睇到,呢度就吾再9up。
 
今日既重点系GrahamScan。
首先介绍极角排序(PolarAngleSort)
个名好有型,但系其实好简单。
 
首先讲一下咩叫极角(PolarAngle):
举个例子,假设我地以坐标点O(1, 1)为极点,有一个点P(2, 2),P - O = (1, 1),则系该极点既极坐标系里面,P点“极角”就为
45度。
 
 跟住到极角排序:
我地取y坐标最小既点(y坐标相同时取x较小者)作为极点,然后从次小极开始逆时针扫描,同一极角取与极点距离较远者
较近者"舍去",按扫描到既次序得到既点序列就系极角排序之后既序列。
 
之后系GrahamScan算法:
 
算法首先将排序后点集前三个点压入堆栈,然后顺序扫描,判断用栈顶同埋次栈顶元素同,待测点点组成既折线用叉积判定该点
转向,如果转右,就弹出栈顶元素,继续判断直到待测点转左,之后将待测点压入栈顶,证明正确性好长,参考算法导论{=       =}。
 
翻译一下导论里面既伪代码:
GRAGAM_SCAN(Q)                      //Q为原始点集
1 对Q进行极角排序
2 PUSH(p0,S)                               
3 PUSH(p1,S)
4 PUSH(p2,S)                                //S为栈,初始化压入排序后Q前三个点
5 for i <- 3 to m do
7     while 以栈顶,次栈顶及pi组成既角是否右转
           do  POP(S)
8 PUSH(pi, S)
10 return S                                     //最后S即为凸集,顺序为逆时针
 
分析题目:
 
其实就系稳一个凸包,然后放大。
我初先WA问题出系两度:
第一、我将放大捻复杂左,造成精度大缺失,其实就系加上一个半径为L既圆。
第二、无留意一个地方,就系"同一极角取与极点距离较远者较近者舍去",其实根据算法特性,吾一定要舍去(只需要系上面伪代码while
判断中加入判断平行),但系我地要考虑一个特殊情况,就系初始化时候,如果无舍弃,第三个点p2就有可能系与p0p1共线并且无条件
压入S,造成最后统计周长时候多出一段多余既p1p2。所以我选取p2时候加入平行判断,AC。
 
下面贴代码:
8966829 GZHU1006100106 1113 Accepted 232K 32MS C++ 2094B 2011-07-22 15:46:02

 

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAXI 1011
#define sq(x) ((x) * (x)) //Square

struct pt { double x, y; } ps[MAXI];
vector<pt> st;          //Pseudo-Stack {=  =}
int n, len;
double l;

double dlt(pt &a, pt &b, pt &o) //Cross-Product
{
	return (o.x - a.x) * (b.y - a.y) - (b.x - a.x) * (o.y - a.y);
}

double dis(pt &a, pt &b) //Distance
{
	return sqrt(sq(a.x - b.x) + sq(a.y - b.y));
}

bool pac(pt &a, pt &b) //Polar-Angle-Compare
{
	double delta = dlt(a, b, ps[0]);

	if ( delta == 0 && sq(a.x - ps[0].x) + sq(a.y - ps[0].y) > sq(b.x - ps[0].x) + sq(b.y - ps[0].y))
		return 1;
	else if (delta < 0)
		return 1;
	return 0;
}

void pas() //Polar-Angle-Sort
{
	int i, id;

	for (id = 0, i = 1; i < n; i++)
		if ((ps[i].y == ps[id].y && ps[i].x < ps[id].x) ||
			 ps[i].y < ps[id].y)
			id = i;
	swap(ps[0], ps[id]);
	sort(ps + 1, ps + n, pac);
#if 0
		for (i = 0; i < n; i++)
			printf("p%d = [%lf, %lf]\n", i, ps[i].x, ps[i].y);
#endif
}

void ghs() //Graham's-Scan
{
	int i;

	pas();
	len = 2;
	st.clear();
	st.push_back(ps[0]);
	st.push_back(ps[1]);
	for (i = 2; i < n; i++)
		if (dlt(st[1], ps[i], st[0]) != 0)
			break;
	st.push_back(ps[2]);
	for (i++; i < n; i++)
	{
		while (dlt(st[len], ps[i], st[len - 1]) >= 0)
		{
			st.pop_back();
			len--;
		}
		st.push_back(ps[i]);
		len++;
	}
	len++;
#if 0
		for (i = 0; i < len; i++)
			printf("p%d = [%lf, %lf]\n", i, st[i].x, st[i].y);
#endif
}

double sta() //Statistic
{
	int i;
	double tsu = 0.0;

	for (i = 0; i < len; i++)
		tsu += dis(st[i % len], st[(i + 1) % len]);
		
#if 0
	printf("%lf\n", dlt(st[1], st[2], st[0]) / (dis(st[0], st[1]) * dis(st[1], st[2])));
#endif

	return tsu + 4.0 * asin(1.0) * l;
}

int main()
{
	int i;
#if 0
	printf("%lf\n", 4.0 * asin(1.0) * 100);
#endif
	while (scanf("%d%lf", &n, &l) != EOF)
	{
		for (i = 0; i < n; i++)
			scanf("%lf%lf", &ps[i].x, &ps[i].y);
		ghs();
		printf("%d\n", (int)(sta() + 0.5));
	}

	return 0;
}

 

第一次用代码控件…………

今日学到好多几何野,想写好多野,以后再写写吧,关于几何既野{=__.=+}
大家都可以去101majia既关于PKU1113题目文章果度参考下噶!
分享到:
评论

相关推荐

    pku_training.utf8

    此数据集用于NLP中分词训练使用,文档中的文字已经人工切分好词组,总共有65536个中国汉字组合而成

    PKU-ACM.rar_PKU_acm 题目

    在编程竞赛的世界里,北京大学(PKU)的ACM团队以其高质量的题目和独特的解题思路闻名。"PKU-ACM.rar"这个压缩包包含了北大ACM题目的一些核心知识点,旨在帮助参赛者理解和掌握算法竞赛中的生命周期题目解法。本文将...

    pku 1113 代码

    ### PKU 1113 代码解析:计算凸包周长 #### 问题背景与描述 本题属于计算几何中的经典问题之一——求解一个点集构成的凸包的周长。凸包(Convex Hull)是指在平面上给定一组点时,将这些点包围起来的所有边构成的...

    EMF.Eclipse Modeling Framework.2nd.PKU.SEI.SA.part1

    EMF.Eclipse Modeling Framework.2nd.PKU.SEI.SA.part1

    EMF.Eclipse Modeling Framework.2nd.PKU.SEI.SA.part2

    EMF.Eclipse Modeling Framework.2nd.PKU.SEI.SA.part2

    穿梭到北大未名(bbs.pku.edu.cn)的穿梭服务器程序

    本软件是一个穿梭到北大未名(bbs.pku.edu.cn)的穿梭服务器程序。北大校内用安装本软件后,别人就可以通过telenet您自己的机器而登陆到北大未名bbs了. 本软件占用内存极少,通常是1到2M; 占用CPU时间约为0.01%。 A....

    ACM.PKU解题报告.part2

    ACM.PKU解题报告.part2

    acm_pku_code.zip_Code p_acm pku_acm pku pu_acm.pku_pku acm

    "p_acm"、"pku_acm"以及"pu_acm.pku_pku"等标签可能是用于分类或标识这些代码属于北京大学(Peking University, PKU)的ACM训练题目。"acm"一词重复出现,进一步强调了这是关于ACM编程竞赛的内容。 描述中提到"acmer...

    深度优先遍历 搜索算法 ACM PKU 1011 S.doc

    深度优先遍历 搜索算法 ACM PKU 1011 S.doc

    PKU_2411.rar_2411 p_pku 2411

    标题中的“PKU_2411.rar_2411 p_pku 2411”似乎是指北京大学(Peking University, 简称PKU)的一个课程或比赛编号2411,其中包含了“rar”后缀的压缩文件。这个压缩包可能包含了该课程或活动的相关资料,如作业、项目...

    PKU+ACM.rar_ACM_PKU_acm pku_acm 北大_site:www.pudn.com

    北京大学(PKU)在计算机科学领域享有盛誉,其在ACM(国际大学生程序设计竞赛,International Collegiate Programming Contest, ICPC)方面的成就更是备受瞩目。"PKU+ACM.rar"这个压缩包显然包含了与北大ACM竞赛相关...

    数据结构与算法课件 北大张铭 DS_02

    - 实验班讲义和作业发布(包括实习作业):`http://db.pku.edu.cn/mzhang/ds/honor/` - 实习课讲义和作业发布:`http://db.pku.edu.cn/mzhang/DS/shixi/` 这些资源提供了学习者获取课程材料及提交作业的方式,有...

    pku题目分类.doc

    这份"Pku题目分类.doc"文件提供了北京大学(Peking University, PKU)编程竞赛中的一些题目分类,涵盖了许多不同的算法和问题类型。以下是对这些标签和题目类型的详细解释: 1. **送分题**:这类题目通常相对简单,...

    PKU的csapp.zip

    这个名为"PKU的csapp.zip"的压缩包文件显然包含了与北京大学(Peking University,简称PKU)相关的CSAPP课程相关的学习资源,特别是针对期末考试的复习材料。 首先,我们可以从"体系结构"这一标签中挖掘出以下几个...

    pku_ACM.rar_PKU_PKU_ACM

    《北京大学PKU ACM竞赛题目解析》 北京大学(Peking University, 简称PKU)作为中国顶级学府,其在计算机科学领域的教育和竞赛有着深厚的底蕴。ACM(International Collegiate Programming Contest,国际大学生程序...

    ACM-PKU-DP.zip_源码

    标题中的"ACM-PKU-DP.zip_源码"表明这是一个与算法竞赛相关的压缩包,特别是北京大学(PKU)的动态规划(DP)问题的源代码集合。动态规划是一种解决复杂问题的有效方法,通常用于优化决策过程,通过将大问题分解为多...

    苯丙酮尿症PKU实施方案.pptx

    苯丙酮尿症(PKU)是一种罕见的遗传性代谢疾病,主要影响氨基酸代谢路径,导致苯丙氨酸无法正常转化为酪氨酸,进而积累过多的苯丙酮酸在体内,对神经系统造成损害,尤其是对儿童的智力发展有严重影响。新生儿疾病...

    ACM.PKU解题报告.part1

    ACM PKU 解题报告 锻炼算法的好东西! 前面的很多题的解题报告。

    ACM学习资料汇总,ACMer要试试哦!

    - [题目2](http://acm.pku.edu.cn/JudgeOnline/problem?id=2288) —— 经典的旅行商问题(TSP),需要考虑如何找到一个最小化的环路。 - [题目3](http://acm.pku.edu.cn/JudgeOnline/problem?id=2411) —— 状态压缩...

Global site tag (gtag.js) - Google Analytics