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

结果四舍五入就可以了
解题思路:题意其实就是在一个凸包外建立一座围墙,要求围墙到凸包(城堡)的最小距离为L,求此围墙的最小总长度。公式:围墙的最小总长度=凸包周长+一个以L为半径的圆周长。证明:假设顺时针给出4个点A、B、C、D(都是凸包的顶点)组成一个凸四边形ABCD。不妨过A点作AE、AF分别垂直于AB、AD,过B点作BG、BH分别垂直于AB、BC......过A点以L为半径作一段弧连到AF,同理使GH成为一段弧。显然EG平行且等于AB......对其他顶点进行同样的操作后,可得出围墙的最小值=四边形的周长+四个顶点对应的弧长(半径都为L)之和。如图所示:
推广到任意凸多边形,每段圆弧都是以凸包上每个对应的顶点为圆心,给定的L为半径,与相邻两条边的切线之间的一段圆弧。每段圆弧的两条半径的夹角与凸包内对应的内角互补。设凸包上有n个顶点,则组成了n个圆周角,总角度数为360°*n=2*180°*n,凸包(凸多边形)的内角和为180°*(n-2),作了2*n条垂线,那么所有垂角之和为2*n*90°=180°*n,因此所有小圆弧对应的圆心角之和为2*180°*n-180°*(n-2)-180°*n=360°(为一个以L为半径的圆)。综上所述,围墙的最小总长度=凸包周长+一个以L为半径的圆周长。
AC代码(63ms):Graham-scan算法:时间复杂度为0(nlogn)。
 #include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const double PI=acos(-1.0);
struct node{int x,y;};
node vex[maxn];
node stackk[maxn];
bool cmp1(node a,node b){
if(a.y==b.y)return a.x<b.x;
else return a.y<b.y;
}
bool cmp2(node a,node b){
double A=atan2(a.y-stackk[].y,a.x-stackk[].x);
double B=atan2(b.y-stackk[].y,b.x-stackk[].x);
if(A!=B)return A<B;
else return a.x<b.x;
}
int cross(node p0,node p1,node p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(node a,node b){
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
int main(){
int n,l;
while(~scanf("%d%d",&n,&l)){
for(int i=;i<n;++i)//输入t个点
scanf("%d%d",&vex[i].x,&vex[i].y);
memset(stackk,,sizeof(stackk));
sort(vex,vex+n,cmp1);
stackk[]=vex[];
sort(vex+,vex+n,cmp2);
stackk[]=vex[];
int top=;
for(int i=;i<n;++i){
while(top>&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
double s=;
for(int i=;i<=top;++i)
s+=dis(stackk[i-],stackk[i]);
s+=dis(stackk[top],vex[]);
s+=*PI*l;//加上圆的周长
printf("%d\n",(int)(s+0.5));//四舍五入
}
return ;
}

AC代码二(32ms):Andrew算法:时间复杂度为O(nlogn),但比Graham-scan算法还快!

 #include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const double PI=acos(-1.0);
struct node{int x,y;}vex[maxn],stackk[maxn];
bool cmp(node a,node b){//坐标排序
return ((a.y<b.y)||(a.y==b.y&&a.x<b.x));
}
int cross(node p0,node p1,node p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(node a,node b){
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
int main(){
int n,l;
while(~scanf("%d%d",&n,&l)){
for(int i=;i<n;++i)
scanf("%d%d",&vex[i].x,&vex[i].y);
memset(stackk,,sizeof(stackk));
sort(vex,vex+n,cmp);
int top=-;
for(int i=;i<n;++i){//构造凸包下侧
while(top>&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
for(int i=n-,k=top;i>=;--i){//构造凸包上侧
while(top>k&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
double s=;
for(int i=;i<=top;++i)//计算凸包周长
s+=dis(stackk[i-],stackk[i]);
s+=*PI*l;
printf("%d\n",(int)(s+0.5));
}
return ;
}

题解报告:poj 1113 Wall(凸包)的更多相关文章

  1. poj 1113 Wall 凸包的应用

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

  2. POJ 1113 Wall 凸包 裸

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

  3. POJ 1113 Wall 凸包求周长

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

  4. POJ 1113 - Wall 凸包

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

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

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

  6. POJ 1113 Wall(凸包)

    [题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...

  7. POJ 1113 Wall【凸包周长】

    题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  8. poj 1113:Wall(计算几何,求凸包周长)

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28462   Accepted: 9498 Description ...

  9. POJ 1113 Wall 求凸包

    http://poj.org/problem?id=1113 不多说...凸包网上解法很多,这个是用graham的极角排序,也就是算导上的那个解法 其实其他方法随便乱搞都行...我只是测一下模板... ...

随机推荐

  1. js对闭包的理解

    原文链接http://www.jb51.net/article/24101.htm,讲的很好,清晰明了.

  2. JS判断按时间跳转到相应的页面

    <!--时间段跳转js--><script language="javaScript" type="text/javascript"> ...

  3. IDEA maven dependency自动提示

    通过File->setting->maven->repositories,选择本地仓库,点击右上角更新,更新maven仓库索引 在pom.xml编写引入依赖的jar包时,已经下载到本 ...

  4. html5--6-40 CSS边框

    html5--6-40 CSS边框 实例 div动态阴影 学习要点 掌握CSS边框属性的使用 元素的边框就是围绕元素内容和内边距的一条或多条线. 元素的边框属性: border 简写属性,用于把针对四 ...

  5. Python学习笔记_Mysql数据库、Excel

    一.操作mysql数据库 import pymysql # 1.连上数据库:账号,密码,ip,端口号,数据库 # 2.建立游标(去数据库拿东西的工人) # 3.执行sql # 4.获取结果 # 5.关 ...

  6. Digit(湘潭大学比赛)

    题目链接: 点击打开链接 中文问题目就不解释了. 思路,找到这个数对应的的数字是多少,然后对这个数取对应的位置. 步骤:先打表打出一位数字对应字符串的长度,两位数的,到8,9就差不多了. 先确定给定的 ...

  7. 百度地图API应用之获取用户的具体位置

    功能的大概:用户通过点击地图上面的位置,在地图上面进行描点,然后再把获取的到的地理位置保存到地图上面的地址栏目中. 主要是百度地图API的使用 .代码如下: var map = new BMap.Ma ...

  8. GCD深入理解(1)

    写在前面 本文原文为raywenderlich的<grand-central-dispatch-in-depth-part-1>:顺便提及一下,笔者认为,对于iOS初学者而言,raywen ...

  9. 【POJ 2411】 Mondriaan's Dream

    [题目链接] 点击打开链接 [算法] 很明显,我们可以用状态压缩动态规划解决此题 f[n][m]表示n-1行已经放满,第n行状态为m的合法的方案数 状态转移方程很好推 注意这题时限较紧,注意加一些小优 ...

  10. Cocos2d-X对常用Object-C特性的替换

    平台的转换,总是让我们不自觉的去寻找两者的相同处,不过Cocos2d-X的确对很多Object-C的特性进行了模仿性质的封装,使熟悉Object-C的人能够在其中看到很多类似的概念而感到亲切.     ...