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.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.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
求出圍住這個城堡的圍牆的最小值,圍牆距離城堡最低不能小於l
所以結果= 城堡的凸包邊長 + l的圓周長。
在這個題中求凸包的時候,進行極角排序->壓入節點->分類如果mul(q2,q1,p[i]) = 1 直接壓入->= 0 拋出棧首的節點,壓入新節點-> = -1只拋出棧首的節點i--。
#include#include #include #include #include using namespace std ; #define eps 1e-8 #define PI 3.141592653589793238462643383279502884197169399375105820974944 struct node{ double x , y ; }p[11000] , q , q1 , q2 ; stack sta ; node sub(node a,node b) { a.x -= b.x ; a.y -= b.y ; return a ; } int mul(node q,node a,node b) { a = sub(a,q) ; b = sub(b,q) ; double x = a.x*b.y - a.y*b.x ; if( fabs(x) < eps ) return 0 ; else if( x > 0 ) return 1 ; return -1 ; } int dis(node q,node a,node b) { a = sub(a,q) ; b = sub(b,q) ; double k1 = a.x*a.x + a.y*a.y , k2 = b.x*b.x + b.y*b.y ; if( fabs(k1-k2) < eps ) return 0 ; else if( k1-k2 > eps ) return 1 ; return -1 ; } int cmp(node a,node b) { int k1 = mul(q,a,b) , k2 = dis(q,a,b) ; return k1 == 1 || (k1 == 0 && k2 == -1) ; } int main() { int n , i , j ; double r , ans ; while( scanf("%d %lf", &n, &r) != EOF ) { q.x = q.y = 11000.0 ; for(i = 0 ; i < n ; i++) { scanf("%lf %lf", &p[i].x, &p[i].y) ; if( p[i].y < q.y || ( fabs(p[i].y-q.y ) < eps && p[i].x < q.x ) ) q = p[i] ; } sort(p,p+n,cmp) ; while( !sta.empty() ) sta.pop() ; sta.push(p[0]) ; sta.push(p[1]) ; for(i = 2 ; i < n ; i++) { //printf("%lf %lf*\n", p[i].x, p[i].y) ; q1 = sta.top() ; sta.pop() ; q2 = sta.top() ; sta.pop() ; int k = mul(q2,q1,p[i]) ; if( k == 1 ) { sta.push(q2) ; sta.push(q1) ; sta.push(p[i]) ; } else if( k == 0 ) { sta.push(q2) ; sta.push(p[i]) ; } else { sta.push(q2) ; i-- ; } } n = 1 ; while( !sta.empty() ) { p[n++] = sta.top() ; //printf("%lf %lf\n", p[n-1].x, p[n-1].y); sta.pop() ; } ans = 2.0*PI*r ; for(i = 0 ; i < n-1 ; i++) { ans += sqrt( (p[i].x-p[i+1].x)*(p[i].x-p[i+1].x)*1.0 + (p[i].y-p[i+1].y)*(p[i].y-p[i+1].y)*1.0 ) * 1.0 ; } printf("%.0f\n", ans) ; } return 0; }