UVa : 11096 (Nails)



// http://uva.onlinejudge.org/external/110/11096.html

Notes : 
1. Classic convex hull problem 

2. There can be only one nail. In that case return the initial length.

3. if there are 2 nails, return distance between them * 2

4. if perimeter of convex hull is less than initial length of ribbon, return initial length

5. use data type long long for, 
initial length of ribbon
location of nail

6. try this input :
3
2147483650 1
2147483650 2147483659

2147483647 3
2147483647 0
-2147483647 0
-234 -234

1 2
0 0
0 1

output :
2147483650.00000
8589934588.00002
2.00000


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
using namespace std;

struct Point {
    long long x;
    long long y;
    double angle;
} p [100 + 5], hull [100 + 5];

double angle (long long x, long long y)
{
    double theta = atan2 (fabs (y), fabs (x));

    if ( x >= 0 ) return theta;
    return pi - theta;
}

bool cmp (Point a, Point b)
{
    if (fabs (a.angle - b.angle) < 1e-6 )
        return (a.x * a.x + a.y * a.y) < (b.x * b.x + b.y * b.y);
    return a.angle < b.angle;
}

bool isInRight (Point a, Point b, Point c)
{
    if ( c.x * (a.y - b.y) + c.y * (b.x - a.x) + (a.x * b.y - a.y * b.x) < 0 )
        return true;
    return false;
}

double square (double a)
{
    return a * a;
}

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        long long initialLength;
        int totalPoints;

        cin >> initialLength >> totalPoints;

        for ( int i = 0; i < totalPoints; i++ )
            cin >> p [i].x >> p [i].y;

        // special case if only 1 totalPoints
        if ( totalPoints == 1 ) {
            printf ("%.5lf\n", (double) initialLength);
            continue;
        }

        int id = 0;

        for ( int i = 1; i < totalPoints; i++ )
            if ( p [i].y < p [id].y ) id = i;

        swap (p [id], p [0]);

        // scaling down respective to id
        for ( int i = totalPoints - 1; i >= 0; i-- ) {
            p [i].x -= p [0].x;
            p [i].y -= p [0].y;
        }

        // measure angle according to id
        for ( int i = 1; i < totalPoints; i++ )
            p [i].angle = angle (p [i].x, p [i].y);

        sort (p + 1, p + totalPoints, cmp);

        hull [0] = p [0];
        hull [1] = p [1];
        int top = 1;

        for ( int i = 2; i < totalPoints; i++ ) {
            while (isInRight (hull [top - 1], hull [top], p [i]))
                top--;
            hull [++top] = p [i];
        }

        double finalLength = 0;

        // perimeter of convex hull
        for ( int i = 1; i <= top; i++ )
            finalLength += sqrt (square (hull [i - 1].x - hull [i].x) + square (hull [i - 1].y - hull [i].y));

        finalLength += sqrt (square (hull [top].x - hull [0].x) + square (hull [top].y - hull [0].y));

        if ( finalLength < initialLength ) finalLength = initialLength;

        printf ("%.5lf\n", finalLength);
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s