Cod sursa(job #1650893)

Utilizator alexb97Alexandru Buhai alexb97 Data 11 martie 2016 21:23:00
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <algorithm>
#include <fstream>
#include <iomanip>
using namespace std;

#define x second
#define y first

//typedef pair<double, double> Point;

typedef pair<double, double> Point;

ifstream is("infasuratoare.in");
ofstream os("infasuratoare.out");

const int MAX_N = 120005;

int n, top;
Point v[MAX_N];
Point stk[MAX_N];

void SortPoints();
void Write();

inline double CrossProduct(const Point& A, const Point& B, const Point& C)
{
    return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

inline int Cmp(const Point& p1, const Point& p2)
{
    return CrossProduct(v[1], p1, p2) > 0;
}


void ConvexHull()
{
    SortPoints();

    stk[1] = v[1];
    stk[2] = v[2];
    top = 2;
    for (int i = 3; i <= n; ++i)
    {
        while (top >= 2 && CrossProduct(stk[top - 1], stk[top], v[i]) < 0)
            --top;
        stk[++top] = v[i];
    }
}
int main()
{
    is >> n;
    for(int i = 1; i <= n; ++i)
    {
        is >> v[i].x >> v[i].y;
    }
    ConvexHull();
    Write();
    is.close();
    os.close();
    return 0;
}


void SortPoints()
{
    int pos = 1;
    for(int i = 1; i <= n; ++i)
    {
        if(v[i] < v[pos])
        {
            pos = i;
        }
    }
    swap(v[1], v[pos]);
    sort(v + 2, v + n + 1, Cmp);
}


void Write()
{
    os << fixed;
    os << top << '\n';
    for (int i = 1; i <= top; ++i )      // in stiva sunt exact in ordine trigonometrica
        os << setprecision(9) << stk[i].x << ' ' << stk[i].y << '\n';
}