EOJ1189 Minimum Convex bag

Source: Internet
Author: User

Once Upon a time there is a greedy King who ordered Hischief Architect to build a wall around the King ' s castle. The King was sogreedy, so he would not listen to his Architect's proposals to build abeautiful brick wall with a perfect Shape and nice tall towers. Instead, heordered to build the wall around the whole castle using the least amount ofstone and labor, but demanded that T He wall should not come closer to thecastle than a certain distance. If the King finds that the Architect have Usedmore resources to build the wall than it is absolutely necessary to SATISFYT Hose requirements, then the Architect'll loose his head. Moreover, hedemanded Architect to introduce in once a plan of the wall listing the exactamount of resources that is Neede D to build the wall.

Your task is to help poor Architect to save his head, by writing a program Thatwill find the minimum possible length of th E wall that he could the build Aroundthe castle to satisfy King's requirements.

The task is somewhat simplified by the fact, that the King's castle has apolygonal shape and are situated on a flat ground. The Architect has alreadyestablished a Cartesian coordinate system and have precisely measured thecoordinates of all Castl E ' s vertices in feet.

input

Input contains Several test cases. The first line of all case contains Twointeger numbers N and L separated by a space. N (3 <= n <=) is thenumber of vertices in the King's Castle, and L (1 <= l <=) is theminimal number of feet that King allows for the wall to come close to Thecastle.

Next N Lines describe coordinates of Castle ' s vertices in a clockwise order. Each line contains-numbers XI and Yi separated by a space ( -10000<= Xi, Yi <= 10000) that represent the coordinates of ITH vertex. Allvertices is different and the sides of the castle do not intersect anywhereexcept for vertices.

Process to end of file.

Output

For each case in the input, write to the output file the single number thatrepresents the minimal possible length of the W All in feet that could is builtaround the castle to satisfy King's requirements. You must present the integernumber of feet to the King, because the floating numbers is not invented yet. However, you must round the result in such a-a, that's it's accurate to 8inches (1 foot are equal to inches), since the King would not be tolerate largererror 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

Test instructions

To find the perimeter of the smallest convex hull, note that this problem requires the circumference of the outer layer of the convex hull, the length of the convex hull circumference plus a circumference (proof method available polygon inner angle and and supplementary angle formula proof).

The minimum convex hull method is as follows: Find y minimum, y same x the largest point is the origin of the new coordinate system. Sort the points by their angular size (which can be achieved by using a vector fork). On the premise of this arrangement: each side if on the basis of the previous edge of the left to form a convex angle, turn right to form a concave angle. Put the point into the stack, before the stack to ensure that the top element of the stack and the new join point to practice the edge of "left", otherwise constantly out of the stack know to find the point to meet the conditions, or only the original point in the stack. The last point left in the stack is the point that makes up the convex hull, and in turn calculates the edge length.

Sorting complexity is O (Nlogn), with stack filtering convex hull complexity is O (n), the total complexity is O (NLOGN).

#include <iostream> #include <cmath> #include <cstdio> #include <stack> #include <algorithm     > #include <vector>using namespace std;const double exps=1e-9;  Double comparison is best defined as an infinitely small, the errand value is less than an infinite hour equal to the const double pi=3.141592653589793238462643383279;struct point{//Point class double    x, y; Point (Double xx=0,double yy=0): X (xx), Y (yy) {} friend double dis (const points &a,const dot &b) {//two pitch Retu    RN sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));    }};struct vec{//vector class double x, y;        Vec () {} VEC (const point &a,const-&b) {//has two points to construct a loud x=a.x-b.x;    Y=A.Y-B.Y;    } friend double operator* (const vec& a,const vec& b) {//overloaded vector fork, returns a scalar value of return a.x*b.y-a.y*b.x;    }};vector<point> p;//the array of bool CMP (const point &a,const point& B) {//Pole angle order, polar angle equal by Origin point o={0.0,0.0};    Vec X={a,o},y={b,o}; return X*y>exps | | (Fabs (x*y) <exps && dis (a,o) <dis (B,o));} int main () {INT N,i;    Double L;    Point tem;        while (~SCANF ("%d%lf", &n,&l)) {p.clear ();        int k=0;            for (I=0;i<n;++i) {Double A, B;            scanf ("%lf%lf", &a,&b);            P.push_back (Point{a,b}); if (P[i].y<p[k].y | |                (P[i].y==p[k].y && p[i].x>p[k].x))        k=i;//the bottom right point, satisfies the left turn test method} Point o={0.0,0.0};//origin tem=p[k];        P.erase (P.begin () +k);            for (int i=0;i<p.size (); ++i) {p[i].x-=tem.x;        P[I].Y-=TEM.Y;        } sort (P.begin (), P.end (), CMP);        stack<point> st;//Filter Stack st.push (o);                for (int i=0;i<p.size (), ++i) {while (St.size () >1) {//guaranteed to meet left point Now=st.top ();                St.pop ();                Point Pre=st.top ();                St.push (now);                    if (vec{now,pre}*vec{p[i],now}>=0.0) {St.push (p[i]);                Break           } st.pop (); } if (St.size () ==1) St.push (P[i]);//Only the origin, no edge, directly into the stack} double ans=0.0;        Tem=st.top ();        Ans+=dis (tem,o);//statistical perimeter, the remaining point in the stack is the convex hull of the constituent Point St.pop ();            while (!st.empty ()) {point t=st.top ();            Ans+=dis (tem,t);            tem=t;        St.pop (); }




EOJ1189 Minimum Convex bag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.