Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 41143   Accepted: 14068

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

 
思路:就是用所有点构成的凸包,然后将凸包向外推L米就可以了,可以知道将墙直接外推L米,墙的长度没变,但是两堵墙之间会有缝隙,这个如果用直线相连
就会浪费,所以直接用圆弧相连,半径为L,所以:总长度 = 原凸包周长 + 所有圆弧的长度。其实所有圆弧的长度之和会构成一个半径为L的圆,所以最后的结果: 总长度 = 原凸包周长 + 圆的周长(R=L)。关于为什么所有圆弧会拼成一个原,我一也是看了许多博客慢慢理解的,证明如下:
 

证明:

先从简单的例子看起:假设现在的凸包有四个顶点构成,可以就一个顶点来观察,我们可以看到此处的周角由四个部分组成:2个直角,一个凸包内角,一个圆弧对应的圆心角。

同理每个顶点都有类似的关系,同时周角固定为360度,而凸包内角和为(4-2)*180 ;

所以总的圆弧对应的圆心角 = 4个周角 - 4 * 2个直角 - 4个凸包内角 = 4 * 360 - 4 * 2 * 90 - (4-2)*180 = 1440-720-360 =360度。

现在推广到n个顶点的凸包:

则所有内角和  =  周角 * n - n * 2个直角 - (n-2)*180 = 360*n - n *180 - n*180 + 360 = 360度。

故对于任意的凸包,所有的圆弧对应的圆心角之和都为360度,它们构成一个完整的圆。

ac代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define N 10005
const double PI = acos(-1.0); int n,tot;//n为二维平面上点的个数,tot为凸包上点的个数
struct node {
int x,y;
}a[N],p[N]; //p[]用来储存凸包 double dis(node a1,node b1){ //两点间距离公式
return sqrt((a1.x-b1.x)*(a1.x-b1.x)+(a1.y-b1.y)*(a1.y-b1.y) + 0.00);
} //叉积:返回结果为正说明p2在向量p0p1的左边(三点构成逆时针方向);
//为负则相反;为0则三点共线(叉积的性质很重要)
double multi(node p0,node p1,node p2){ //
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} //极角排序:极角排序是根据坐标系内每一个点与x轴所成的角,逆时针比较。按照角度从小到大的方式排序
int cmp(node p1,node p2){ //极角排序;
int x=multi(p1,p2,a[0]);
if(x>0||(x==0&&dis(p1,a[0])<dis(p2,a[0])))
return 1;
return 0;
} void Graham(){ //求凸包
int k=0;
for(int i=0;i<n;i++) //找到最下最左的一个点
if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x))
k=i;
swap(a[0],a[k]); //将其设置为第一个点
sort(a+1,a+n,cmp);
tot=2,p[0]=a[0],p[1]=a[1]; //p[]模拟栈,用来储存凸包
for(int i=2;i<n;i++){
while(tot>1&&multi(p[tot-1],p[tot-2],a[i])>=0)
tot--; //右拐就回退
p[tot++]=a[i]; //左拐就放入
}
} double getArea(){
struct node b[3];
b[0] = p[0], b[1] = p[1], b[2] = p[2];
double area = 0;
for(int i = 2; i < tot; i++){
area += multi(b[0], b[1], p[i]) / 2.0;
b[1] = p[i];
}
return area;
} double getGirth(){
double rt = 0;
for(int i = 0; i < tot; i++){
rt += dis(p[i], p[(i+1)%tot]);
}
return rt;
} int main(){
double L;
while(cin >> n >> L){
tot = 0;
for(int i = 0; i < n; i++){
cin >> a[i].x >> a[i].y;
}
Graham();
double res = getGirth();
res += 2*PI*L;
cout << int(res + 0.5) << endl;
}
return 0;
}

  

81-POJ-Wall(计算几何)的更多相关文章

  1. POJ 1113 Wall(计算几何の凸包)

    Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...

  2. POJ 1556 计算几何+最短路

    代码1: #include<iostream> #include<stdio.h> #include<string> #include<string.h> ...

  3. POJ 2954-Triangle(计算几何+皮克定理)

    职务地址:POJ 2954 意甲冠军:三个顶点的三角形,给出,内部需求格点数. 思考:就像POJ 1265. #include <stdio.h> #include <math.h& ...

  4. poj 1410 计算几何

    /** 注意: 千万得小心..就因为一个分号,调了一个晚上... **/ #include <iostream> #include <algorithm> using name ...

  5. poj 2653 计算几何

    #include <iostream> #include <cstring> #include <algorithm> #include <cmath> ...

  6. poj 1269 计算几何

    /** 判断直线位置关系 **/ #include <iostream> #include <cmath> #include <cstdio> using name ...

  7. poj 3304 计算几何

    大意: 是否存在一条直线,使所有线段在直线上的投影至少交与一点 思路: 转换为是否存在一条直线与所有的线段相交,做这条直线的垂线,那么垂线即为所求 **/ #include <iostream& ...

  8. poj 2398 计算几何

    #include <iostream> #include<cstdio> #include<cstring> #include <algorithm> ...

  9. BZOJ 1113 Wall ——计算几何

    凸包第一题. 自己认为自己写的是Andrew 其实就是xjb写出来居然过掉了测试. 刚开始把pi定义成了int,调了半天 #include <map> #include <cmath ...

  10. POJ 3855 计算几何·多边形重心

    思路: 多边形面积->任选一个点,把多边形拆成三角,叉积一下 三角形重心->(x1+x2+x3)/3,(y1+y2+y3)/3 多边形重心公式题目中有,套一下就好了 计算多边形重心方法: ...

随机推荐

  1. javascript正则表达式定义(语法)

    正则表达式的2种定义方法:一种是直接调用RegExp(),第二种是直接用字面量来定义,即var re = /正则规则/; 2种定义方法本质都是调用RegExp()方法 在调用同一段正则代码的时候,EC ...

  2. [转]VS2010中使用模块定义文件(.def)

    都知道在写DLL的时候,使用模块定义文件(.def)可以防止DLL里的命名变更. vc6.0中只要在当前目录下添加.def文件,然后编译就Ok了 但在vs2010里这样做是不可以的,必须在项目--属性 ...

  3. Struts2.0 xml文件的配置(package,namespace,action)

    struts.xml配置 struts.xml文件是整个Struts2框架的核心. struts.xml文件内定义了Struts2的系列Action,定义Action时,指定该Action的实现类,并 ...

  4. SQL 函数:树结构指定父节点遍历所有的子节点

    CREATE function [dbo].[Get_DepChildren] ( @ID int ) , ),PID ), Name )) as begin --declare @ID Int -- ...

  5. 019:InnoDB 表空间内部组织结构

    一. 表空间内部组织结构 表空间 内部有多个段对象(Segment) 组成 每个段(Segment)由区(Extent) 组成 每个区(Extent)由页(Page)组成 每个页里面报存数据 (或者叫 ...

  6. 5月16日上课笔记-js中DOM操作

    一.DOM操作 DOM节点的操作 增加 删除 修改 节点的信息: nodeName 获取节点的标签名 parentNode 获取父节点 childNodes IE忽略回车换行,chrome回车换行是文 ...

  7. 【BZOJ】1030: [JSOI2007]文本生成器(AC自动机+dp)

    题目 传送门:QWQ 传送到洛谷QWQ 分析 我一开始也不会做这题的,后来看了很多网上的题解,终于AC了.(我好菜啊) 主要参考:传送门QWQ 直接搞非常麻烦,反正我是不会做.于是考虑求反,即求有多少 ...

  8. 初探asciinema

    在学习c++的时候想到so库注入到某些程序中,网上查了一些资料,发现了一些比较好玩的程序,分享一下. 主要可以对操作的进行视频回放,类似堡垒机的功能 安装: pip3 install asciinem ...

  9. OD 实验(二) - 绕过序列号验证

    需要破解的程序 输入用户名和序列号,点击 Check,程序会进行校验 用 OD 打开程序 按快捷键 Ctrl+F9 跟随表达式 GetDlgItemTextA 点击 ok 在这里调用了 GetDlgI ...

  10. python装饰器注意事项

    内容: 1.装饰器基本结构复习 2.装饰器注意事项 python装饰器详细内容:http://www.cnblogs.com/wyb666/p/8748102.html 1.装饰器基本结构复习 装饰器 ...