poj 1113 Wall (凸包模板题)

Wall

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 32808   Accepted: 11137

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

给定n个点,求围一圈围墙使得围墙到每个点的距离大于等于l,求围墙的最小周长。

这道题的关键点是,由于最后是绕着中间的n个点转了一周(2×pi)

每个点对应一定弧度,合起来正好是一个圆的周长。

也就是,最后的答案为凸包的周长加上半径为L的圆的周长。

第一次写凸包,感谢适牛的板子,真是炒鸡好用呀喵喵喵。

不过引用那里没搞明白QAQ,稍微改了下写法。。

  1 /*************************************************************************
  2     > File Name: code/poj/1113.cpp
  3     > Author: 111qqz
  4     > Email: [email protected]
  5     > Created Time: 2015年11月09日 星期一 16时33分28秒
  6  ************************************************************************/
  7
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21 #define fst first
 22 #define sec second
 23 #define lson l,m,rt<<1
 24 #define rson m+1,r,rt<<1|1
 25 #define ms(a,x) memset(a,x,sizeof(a))
 26 using namespace std;
 27 const double eps = 1E-8;
 28 const int dx4[4]={1,0,0,-1};
 29 const int dy4[4]={0,-1,1,0};
 30 typedef long long LL;
 31 const int inf = 0x3f3f3f3f;
 32 const double pi = acos(-1.0);
 33 const int N=1E3+7;
 34 int n,l;
 35 int top;
 36 int dblcmp(int d)
 37 {
 38     return d<-eps?-1:d>eps;
 39 }
 40 struct point
 41 {
 42     double x,y;
 43     point (){}
 44     point (double _x,double _y):
 45     x(_x),y(_y){};
 46
 47     void input()
 48     {
 49     scanf("%lf %lf",&x,&y);
 50     }
 51     point operator - (const point &p) const{
 52     return point (x-p.x,y-p.y);
 53     }
 54     double operator ^ (const point  &p) const{
 55     return x*p.y-y*p.x;
 56     }
 57     bool operator < (const point a)const{
 58     return dblcmp(a.x-x)==0?dblcmp(y-a.y)<0:x<a.x;
 59     }
 60     double distance(point p)
 61     {
 62     return hypot(x-p.x,y-p.y);
 63     }
 64     point sub(point p)
 65     {
 66     return point(x-p.x,y-p.y);
 67     }
 68
 69     double det(point p)
 70     {
 71     return x*p.y-y*p.x;
 72     }
 73
 74
 75 }p[N];
 76
 77
 78 struct polygon
 79 {
 80     point p[N];
 81     int n;
 82     void input()
 83     {
 84
 85     p[0].input();
 86     for ( int i = 1 ; i < n ; i++)
 87     {
 88         p[i].input();
 89         if (p[i].y>p[0].y||(p[i].y==p[0].y&&p[i].x>p[0].x))
 90         swap(p[0],p[i]);
 91     }
 92     }
 93
 94     struct cmp
 95     {
 96     point p;
 97     cmp(const point &p0){p=p0;}
 98     bool operator()(const point &aa,point &bb)
 99     {
100         point a=aa,b=bb;
101         int d = dblcmp((a-p)^(b-p));
102         if (d==0)
103         {
104         return dblcmp(a.distance(p)-b.distance(p))<0;
105         }
106         else
107         return d>0;
108     }
109
110     };
111
112     void getconvex(polygon &convex,int &top)
113     {
114     int i,j,k;
115     sort(p,p+n); //极角排序
116     convex.n=n;
117
118     for ( int i = 0 ; i < min (n,2);i++)
119     {
120         convex.p[i] = p[i];
121     }
122     if (n<=2) return ;
123     top = convex.n;
124     top = 1;
125     for ( int i = 2 ; i < n ; i++)
126     {
127         while (top&&convex.p[top].sub(p[i]).det(convex.p[top-1].sub(p[i]))<=0) top--;
128
129         convex.p[++top]=p[i];
130     }
131
132        int tmp = top;
133        convex.p[++top] = p[n-2];
134        for (int i = n-3 ; i >= 0 ; i--)
135        {
136            while (top!=tmp&&convex.p[top].sub(p[i]).det(convex.p[top-1].sub(p[i]))<=0)
137            top--;
138            convex.p[++top]=p[i];
139        }
140     //   ztop = top;
141
142     };
143
144 }pol;
145
146 int main()
147 {
148   #ifndef  ONLINE_JUDGE
149    freopen("in.txt","r",stdin);
150   #endif
151
152    while (scanf("%d %d",&n,&l)!=EOF)
153     {
154     pol.n=n;
155     pol.input();
156     polygon conv;
157     pol.getconvex(conv,top);
158
159        double res=0;
160 //       cout<<"top:"<<top<<endl;
161        for ( int i = 0 ; i < top ; i++)
162            res +=conv.p[i].distance(conv.p[i+1]);
163        res +=conv.p[0].distance(conv.p[top]);
164
165        res = res + 2*pi*l;
166        printf("%d\n",(int)(res+0.5));
167
168
169
170     }
171
172
173  #ifndef ONLINE_JUDGE
174   #endif
175   fclose(stdin);
176     return 0;
177 }

时间: 2024-12-28 01:44:39

poj 1113 Wall (凸包模板题)的相关文章

POJ - 1113 Wall (凸包模板题)

原题链接 模板题,直接说思路. 思路: 要求一距离凸包为 L 的图形的周长,即为 凸包周长+L为半径的圆周长 ,直接用 Graham 求一次凸包即可. 1 /* 2 * @Author: windystreet 3 * @Date: 2018-08-02 20:41:25 4 * @Last Modified by: windystreet 5 * @Last Modified time: 2018-08-02 22:30:59 6 */ 7 #include <stdio.h> 8 #inc

POJ 1113 - Wall 凸包模板

此题为凸包问题模板题,题目中所给点均为整点,考虑到数据范围问题求norm()时先转换成double了,把norm()那句改成<vector>压栈即可求得凸包. 初次提交被坑得很惨,在GDB中可以完美运行A掉,到OJ上就频频RE(此处应有黑人问号) 后来发现了问题,原因是玩杂耍写了这样的代码 struct point { int x, y; point (){ scanf("%d%d", &x, &y); } ... }pt[MAXN]; 于是乎,在swap

POJ 1113 Wall (凸包)

题目地址:POJ 1113 先求出凸包的周长,然后剩下的弧合起来一定是个半径为l的圆,然后再加上以l为半径的圆的周长即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <

poj 1113 Wall 凸包的应用

题目链接:poj 1113   单调链凸包小结 题解:本题用到的依然是凸包来求,最短的周长,只是多加了一个圆的长度而已,套用模板,就能搞定: AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cmath> 5 using namespace std; 6 int m,n; 7 struct p 8 { 9 double x,y; 10 friend i

POJ 1113 Wall 凸包 裸

LINK 题意:给出一个简单几何,问与其边距离长为L的几何图形的周长. 思路:求一个几何图形的最小外接几何,就是求凸包,距离为L相当于再多增加上一个圆的周长(因为只有四个角).看了黑书使用graham算法极角序求凸包会有点小问题,最好用水平序比较好.或者用Melkman算法 /** @Date : 2017-07-13 14:17:05 * @FileName: POJ 1113 极角序求凸包 基础凸包.cpp * @Platform: Windows * @Author : Lweleth (

POJ 1113 Wall (凸包 + 思维)

题目:传送门 题意:有一个 n 多边形城堡,先需在城堡外建围墙,使得围墙到城堡的距离不得小于 L,且围墙的周长最小. 思路:答案就是凸包周长 + 半径为 L 的圆的周长.   证明 A.B.C.D四个点,每个点都有 360 度, 然后,角1.2.3.4构成多变形的内角和为 360度,然后每个点,又要减去两个90度,那么剩下的就是圆心角5.6.7.8,它们的和为 4 * (360 - 180) - 360 = 360,刚好是一个圆.推广到任意多边形也是类似的. #include <iostream

POJ 1113 凸包模板题

上模板. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <utility> #include <stack> #include <queue> #include <map> #include

POJ 3528 hdu 3662 三维凸包模板题

POJ 3528题:http://poj.org/problem?id=3528 HDU 3662:http://acm.hdu.edu.cn/showproblem.php?pid=3662 一个是求三维凸包面数,一个是求三维凸包表面积,都是很裸的. 贴代码: #include<stdio.h> #include<algorithm> #include<string.h> #include<math.h> #include<stdlib.h>

G++和C++ &amp;&amp; POJ 1113 Wall

PS: 次题目虽然叙述点的个数大于等于3但是并不保证凸包是否存在,所以还要判断一下.经常刷题的孩纸可能会遇到用C++ 可用AC的题目用G++ 却 Wrong Answer. 思考过为什么吗? 对于double 类型用%lf 输入用%lf输出是window 环境下VC的标准但不是真正的标准,对于double 类型 真正的标准是用%lf输入,用%f输出.所以把%.0lf改为%.0f 在G++环境下面就可用轻松AC了. 还有%lld 和 %I64d, 同时也学习一下控制精度的技巧,比如 printf(