HDU 1348 Wall (水平序Graham算法)
2016-02-07 13:58
316 查看
Wall
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4774 Accepted Submission(s): 1370
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
[align=left]Sample Input[/align]
1 9 100 200 400 300 400 300 300 400 300 400 400 500 400 500 200 350 200 200 200
[align=left]Sample Output[/align]
1628
Graham算法
总长就是凸包的长度加上PI * 2 * L,至于怎么证明。。。
对了,输出的格式也要特别注意
下面是代码
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack> #include <set> #define PI (atan(1.0) * 4) using namespace std; struct point { int x, y; }; bool cmp(point a, point b) { if(a.x != b.x) return a.x < b.x; else return a.y < b.y; } double Graham(point * ver, int N) { stack<point> S; while(!S.empty()) { S.pop(); } S.push(ver[0]); S.push(ver[1]); int i; int flag; point t1, t2; for(i = 2; i < N; i++) { flag = 0; t1 = S.top(); S.pop(); t2 = S.top(); S.push(t1); if((t1.x - t2.x) * (ver[i].y - t1.y) - (ver[i].x - t1.x) * (t1.y - t2.y) <= 0) { S.pop(); flag = 1; } if(S.size() < 2 || flag == 0) { S.push(ver[i]); } else { i--; } } double len = 0; t1 = S.top(); S.pop(); while(!S.empty()) { t2 = S.top(); S.pop(); len += sqrt((t1.x - t2.x) * (t1.x - t2.x) * 1.0 + (t1.y - t2.y) * (t1.y - t2.y) * 1.0); t1 = t2; } return len; } int main() { int T; point vertices[1010]; point rvertices[1010]; while(~scanf("%d", &T)) { int N, L; while(T--) { scanf("%d %d", &N, &L); int i; for(i = 0; i < N; i++) { scanf("%d %d", &vertices[i].x, &vertices[i].y); } sort(vertices, vertices + N, cmp); int j; for(i = 0, j = N - 1; i < N; i++, j--) { rvertices[i] = vertices[j]; } double len1 = Graham(vertices, N); double len2 = Graham(rvertices, N); printf("%.0lf\n", len1 + len2 + PI * (2 * L)); if(T) printf("\n"); } } return 0; }
相关文章推荐
- Styling Views on Android
- 求解最大正方形面积 — leetcode 221. Maximal Square
- Vim简明教程【CoolShell】转自http://blog.csdn.net/niushuai666/article/details/7275406 (飘过的小牛)
- HDU 5621 KK's Point
- Win10系统查看网络连接详细信息的方法
- Leetcode 322. Coin Change
- hdu KK's Steel【思维】【斐波那契数列应用】
- 1238 Substrings
- Activity
- 学习Hibernate源码——配置文件解析,SessionFactory创建
- [置顶] 宿主机-开发板设置NFS共享目录(1)
- 大数据IMF 传奇 8台设备如何实现免密码的SSH登录呢 ?脚本分发 解决方案
- 自学帝国CMS---网站搬家
- HDU3037 Saving Beans(Lucas定理+乘法逆元)
- BestCoder Round #71 (div.2)-KK's Steel(斐波那契数列)
- [Spring实战系列](12)Bean的自动装配
- 递推-hdu1041 Computer Transformation
- 在html在添加cookie和读取cookie
- hdu 1598 find the most comfortable road 枚举+并查集
- WMI技术介绍和应用——Instance/Method Provider