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

Northeastern Europe 2001

简化下题意即求凸包的周长+2×PI×r。

这道题的答案是凸包周长加上一个圆周长,即包围凸包的一个圆角多边形,但是没弄明白那些圆角加起来为什么恰好是一个圆。每个圆角是以凸包对应的顶点为圆心,给定的L为半径,与相邻两条边的切点之间的一段圆弧。每个圆弧的两条半径夹角与对应的凸包的内角互补。假设凸包有n条边,则所有圆弧角之和为180°n-180°(n-2)=360°。故,围墙周长为=n条平行于凸包的线段+n条圆弧的长度=凸包周长+围墙离城堡距离L为半径的圆周长。

AC代码:学习自KB大佬的模板加以修改

// Author : RioTian
// Time : 20/10/21
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1000;
const double Pi = acos(-1.0); struct point {
int x, y;
point() : x(), y() {}
point(int x, int y) : x(x), y(y) {}
} list[N];
typedef point P;
int stack[N], top;
//计算叉积: p0p1 X p0p2
int cross(P p0, P p1, P p2) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
//计算 p1p2的 距离
double dis(P p1, P p2) {
return sqrt((double)(p2.x - p1.x) * (p2.x - p1.x) +
(p2.y - p1.y) * (p2.y - p1.y));
}
//利用极角排序,角度相同则距离小排前面
bool cmp(P p1, P p2) {
int tmp = cross(list[0], p1, p2);
if (tmp > 0)
return true;
else if (tmp == 0 && dis(list[0], p1) < dis(list[0], p2))
return true;
else
return false;
}
//输入,并把 最左下方的点放在 list[0] 。并且进行极角排序
void init(int n) {
int i, k = 0;
cin >> list[0].x >> list[0].y;
P p0 = list[0]; // p0 等价于 tmp 去寻找最左下方的点
for (int i = 1; i < n; ++i) {
cin >> list[i].x >> list[i].y;
if (p0.y > list[i].y || (p0.y == list[i].y && p0.x > list[i].x))
p0 = list[i], k = i;
}
list[k] = list[0];
list[0] = p0;
sort(list + 1, list + n, cmp);
}
//graham扫描法求凸包,凸包顶点存在stack栈中
//从栈底到栈顶一次是逆时针方向排列的
//如果要求凸包的一条边有2个以上的点
//那么要将while中的<=改成<
//但这不能将最后一条边上的多个点保留
//因为排序时将距离近的点排在前面
//那么最后一条边上的点仅有距离最远的会被保留,其余的会被出栈
//所以最后一条边需要特判
//如果要求逆凸包的话需要改cmp,graham中的符号即可
void Graham(int n) {
int i;
if (n == 1) top = 0, stack[0] = 0;
if (n == 2) top = 1, stack[0] = 0, stack[1] = 1;
if (n > 2) {
for (i = 0; i <= 1; i++) stack[i] = i;
top = 1; for (i = 2; i < n; i++) {
while (top > 0 &&
cross(list[stack[top - 1]], list[stack[top]], list[i]) <= 0)
top--;
top++;
stack[top] = i;
}
}
}
int main() {
// freopen("in.txt", "r", stdin);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int N, L;
while (scanf("%d%d", &N, &L) != EOF) {
init(N);
Graham(N);
//叉积求凸包面积
double res = 0;
for (int i = 0; i < top; i++)
res += dis(list[stack[i]], list[stack[i + 1]]);
res += dis(list[stack[0]], list[stack[top]]); res += 2 * Pi * L;
printf("%d\n", (int)(res + 0.5));
}
}

POJ - 1113 Wall (凸包模板) Graham Scan 算法实现的更多相关文章

  1. POJ 1113 Wall 凸包 裸

    LINK 题意:给出一个简单几何,问与其边距离长为L的几何图形的周长. 思路:求一个几何图形的最小外接几何,就是求凸包,距离为L相当于再多增加上一个圆的周长(因为只有四个角).看了黑书使用graham ...

  2. poj 1113 Wall 凸包的应用

    题目链接:poj 1113   单调链凸包小结 题解:本题用到的依然是凸包来求,最短的周长,只是多加了一个圆的长度而已,套用模板,就能搞定: AC代码: #include<iostream> ...

  3. POJ 1113 - Wall 凸包

    此题为凸包问题模板题,题目中所给点均为整点,考虑到数据范围问题求norm()时先转换成double了,把norm()那句改成<vector>压栈即可求得凸包. 初次提交被坑得很惨,在GDB ...

  4. POJ 1113 Wall 凸包求周长

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26286   Accepted: 8760 Description ...

  5. poj 1113 wall(凸包裸题)(记住求线段距离的时候是点积,点积是cos)

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43274   Accepted: 14716 Descriptio ...

  6. 凸包Graham Scan算法实现

    凸包算法实现点集合中搜索凸包顶点的功能,可以处理共线情况,可以输出共线点也可以不输出而只输出凸包顶点.经典的Graham Scan算法,点排序使用极角排序方式,并对共线情况做特殊处理.一般算法是将共线 ...

  7. 凸包问题 Graham Scan

    2020-01-09 15:14:21 凸包问题是计算几何的核心问题,并且凸包问题的研究已经持续了好多年,这中间涌现出了一大批优秀的算法. 凸包问题的最优解法是Graham Scan算法,该算法可以保 ...

  8. 凸包模板——Graham扫描法

    凸包模板--Graham扫描法 First 标签: 数学方法--计算几何 题目:洛谷P2742[模板]二维凸包/[USACO5.1]圈奶牛Fencing the Cows yyb的讲解:https:/ ...

  9. 计算几何--求凸包模板--Graham算法--poj 1113

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28157   Accepted: 9401 Description ...

  10. POJ 1113 Wall 求凸包的两种方法

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31199   Accepted: 10521 Descriptio ...

随机推荐

  1. Vue03-组件化

    01. 组件化思想 当我们面对一个复杂问题的时候,常见的.高效的做法就是对复杂问题进行拆分, 将复杂问题拆分成一个个小的.简单的问题, 逐一解决小问题,再将处理好的小问题整合到一起, 如此解决复杂问题 ...

  2. 【Javaweb】四(关于接口类的作用)

    这里我们还是以房产信息管理系统的题目举例: 发现在DAO层和service层都有接口类(注:impl是实现类) 为什么要用接口,不直接写实现类: 1.简单.规范性:这些接口不仅告诉开发人员你需要实现那 ...

  3. class-dump 混淆加固、保护与优化原理

    ​ class-dump 混淆加固.保护与优化原理 进行逆向时,经常需要dump可执行文件的头文件,用以确定类信息和方法信息,为hook相关方法提供更加详细的数据.class-dump的主要用于检查存 ...

  4. nacos 安装和使用

    Nacos 是阿里巴巴开源项目,用于构建微服务应用的服务发现.配置管理和服务管理. 在微服务项目中不同模块之间服务调用时,实现服务注册与发现. Nacos 使用: Nacos 是java开发的,依赖 ...

  5. gridlayout

    <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android=" ...

  6. Rabbit加密算法

    一.引言 随着信息技术的快速发展,数据安全已成为越来越受到重视的领域.加密算法作为保障数据安全的重要技术手段,在通信.存储等领域得到了广泛应用.Rabbit加密算法作为一种新型的加密算法,凭借其简单易 ...

  7. ASR项目实战-产品分析

    分析Google.讯飞.百度.阿里.QQ.搜狗等大厂的ASR服务,可以罗列出一款ASR服务所需要具备的能力. 产品分类 ASR云服务产品,从用户体验.时效性.音频时长,可以划分为如下几类: 实时短音频 ...

  8. 2024-01-06:用go语言,在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧 在桥上有一些石子,青蛙很讨厌踩在这些石子上 由于桥的长度和青蛙一次跳过的距离都是正整数 我们可以把独木桥

    2024-01-06:用go语言,在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧 在桥上有一些石子,青蛙很讨厌踩在这些石子上 由于桥的长度和青蛙一次跳过的距离都是正整数 我们可以把独木桥 ...

  9. Java通过SSH连接路由器,输入命令并读取响应

    最近需要读取和修改华为路由器的配置,使用Java语言开发,通过SSH连接,输入命令并读取响应. 1.添加mwiede/jsch依赖 如果使用Maven,可以在pom.xml文件中添加以下依赖: < ...

  10. Mybatis源码3 CachingExecutor, 二级缓存,缓存的实现

    Mybatis CachingExecutor, 二级缓存,缓存的实现 一丶二级缓存概述 上一章节,我们知道mybaits在构造SqlSession的时候,需要让SqlSession持有一个执行器,如 ...