Poj 1113 | HDU 1348: wall (convex hull problem)

Source: Internet
Author: User
Tags integer numbers cmath

Portal:

Poj: Click to open the link.

HDU: Click to open the link.


The following are the questions on poj;


Wall
Time limit:1000 ms   Memory limit:10000 K
Total submissions:29121   Accepted:9746

Description

Once upon a time there was a greedy king who ordered his Chief implements ECT to build a wall around the king's castle. the king was so greedy, That he wocould not listen to his own ect'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 shocould not come closer to the castle than a certain distance. if the King finds that the role ECT has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the role ect will loose his head. moreover, he demanded effecect 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 revoke ECT to save his head, by writing a program that will find the minimum possible length of the wall that he cocould build 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 specified ECT 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 T for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that cocould 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 supported Ted 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 100200 400300 400300 300400 300400 400500 400500 200350 200200 200

Sample output

1628

Hint

The result is rounded to the nearest integer.


The general purpose of this question is to find the smallest perimeter of the surface wrapped with all vertices, and the circumference of the circle with the radius of L ..

Using the Andrew Algorithm


</PRE> <PRE name = "code" class = "CPP"> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector >#include <queue >#include <sstream >#include <cmath> using namespace STD; # define F1 (I, n) for (INT I = 0; I <n; I ++) # define F2 (I, m) for (INT I = 1; I <= m; I ++) # define F3 (I, n) for (INT I = N; I> = 0; I --) # define M 1005 # define PI 3.1415926 struct point {Double X, Y ;}; void sort (point * P, int N) // follow x Sort from small to large (if X is the same, sort by Y from small to large) {point temp; F1 (I, n-1) F1 (J, n-i-1) {If (P [J]. x> P [J + 1]. x) | (P [J]. X = P [J + 1]. X & P [J]. y> P [J + 1]. y) {temp = P [J]; P [J] = P [J + 1]; P [J + 1] = temp ;}}} int cross (INT X1, int Y1, int X2, int Y2) // check whether P [I] is in its internal state .. </Span> {If (x1 * y2-x2 * Y1 <= 0) // The cross product is smaller than 0, indicating that P [I] is on the right of the current forward direction, therefore, you need to remove C M-1], C [m-2] </span> <span> return 0; else return 1;} double DIS (point a, point B) from the convex hull) // calculate the length between Two convex points .. </Span> <span> {return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} int convexhull (point * P, point * C, int N) {int m = 0; F1 (I, n) // lower convex hull </span> <span> {While (M> 1 &&! Cross (C [m-2]. x-C [s-1]. x, C [m-2]. y-C [s-1]. y, C [m-2]. x-P [I]. x, C [m-2]. y-P [I]. y) m --; C [M ++] = P [I];} int K = m; F3 (I, N-2) // top convex hull </span> <span> {While (M> K &&! Cross (C [m-2]. x-C [s-1]. x, C [m-2]. y-C [s-1]. y, C [m-2]. x-P [I]. x, C [m-2]. y-P [I]. y) m --; C [M ++] = P [I];} If (n> 1) m --; return m;} int main () {point A [m], p [m]; double sum; int N, R; while (CIN> N> r) {sum = 0.0; F1 (I, n) scanf ("% lf", & A [I]. x, & A [I]. y); sort (A, n); int M = convexhull (A, P, n); F2 (I, m) sum + = DIS (P [I], P [I-1]); sum + = 2 * pI * r; printf ("%. LF \ n ", sum);} return 0 ;}


 
I don't know why .. I use LF to submit with G ++ to WA, and C ++ to AC .. In the discussion board, we also said that the LF was used to submit an error .. Change it to F .. It is possible that the output of G ++ is f by default ...~~ (Random packet loss) B 

The following is the AC code on HDU .. It is because the PE has been pasted once .. Note the format ..

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<sstream>#include<cmath>using namespace std;#define f1(i, n) for(int i=0; i<n; i++)#define f2(i, m) for(int i=1; i<=m; i++)#define f3(i, n) for(int i=n; i>=0; i--)#define M 1005#define PI 3.1415926struct Point{    double x, y;};void sort(Point *p, int n){    Point temp;    f1(i, n-1)    f1(j, n-i-1)    {        if( (p[j].x > p[j+1].x) || (p[j].x==p[j+1].x && p[j].y>p[j+1].y) )        {            temp = p[j];            p[j] = p[j+1];            p[j+1] = temp;        }    }}int cross(int x1, int y1, int x2, int y2){    if(x1*y2-x2*y1<=0)        return 0;    else        return 1;}double dis(Point a, Point b){    return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );}int convexhull(Point *p, Point *c, int n){    int m = 0;    f1(i, n)    {        while( m>1 && !cross(c[m-2].x-c[m-1].x, c[m-2].y-c[m-1].y, c[m-2].x-p[i].x, c[m-2].y-p[i].y) )            m--;        c[m++] = p[i];    }    int k = m;    f3(i, n-2)    {        while( m>k && !cross(c[m-2].x-c[m-1].x, c[m-2].y-c[m-1].y, c[m-2].x-p[i].x, c[m-2].y-p[i].y) )            m--;        c[m++] = p[i];    }    if(n>1)        m--;    return m;}int main(){    Point a[M], p[M];    double sum;    int t;    while( cin>>t )    {        while( t-- )        {            sum=0.0;            int n, r;            cin>>n>>r;            f1(i, n)            scanf("%lf %lf", &a[i].x, &a[i].y);            sort (a, n);            int m = convexhull(a, p, n);            f2(i, m)            sum+=dis( p[i], p[i-1] );            sum+=2*PI*r;            printf("%.lf\n", sum);            if(t)                printf("\n");        }    }    return 0;}






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.