码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1113 Wall (凸包模板题)

时间:2015-11-10 12:23:42      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

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

 
 
给定n个点,求围一圈围墙使得围墙到每个点的距离大于等于l,求围墙的最小周长。
这道题的关键点是,由于最后是绕着中间的n个点转了一周(2×pi)
每个点对应一定弧度,合起来正好是一个圆的周长。
也就是,最后的答案为凸包的周长加上半径为L的圆的周长。
 
第一次写凸包,感谢适牛的板子,真是炒鸡好用呀喵喵喵。
 
不过引用那里没搞明白QAQ,稍微改了下写法。。
技术分享
  1 /*************************************************************************
  2     > File Name: code/poj/1113.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  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 }
View Code

 

poj 1113 Wall (凸包模板题)

标签:

原文地址:http://www.cnblogs.com/111qqz/p/4952325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!