程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> poj1113

poj1113

編輯:關於C++
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30701 Accepted: 10340

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 poZ喎?/kf/ware/vc/" target="_blank" class="keylink">vciBBcmNoaXRlY3QgdG8gc2F2ZSBoaXMgaGVhZCwgYnkgd3JpdGluZyBhIHByb2dyYW0gdGhhdCB3aWxsIGZpbmQgdGhlIG1pbmltdW0gcG9zc2libGUgbGVuZ3RoIG9mIHRoZSB3YWxsIHRoYXQgaGUgY291bGQgYnVpbGQgYXJvdW5kIHRoZSBjYXN0bGUgdG8gc2F0aXNmeSBLaW5n"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

求出圍住這個城堡的圍牆的最小值,圍牆距離城堡最低不能小於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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved