Cod sursa(job #809919)

Utilizator sebii_cSebastian Claici sebii_c Data 9 noiembrie 2012 13:20:09
Problema Infasuratoare convexa Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <cstdio>

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <vector>

#define x first
#define y second

using namespace std;

typedef pair<double, double> point;

const int MAXN = 120010;

int n;
point v[MAXN];

int head;
point stack[MAXN];

inline double cross_prod(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 bool comp(const point& A, const point& B)
{
    return cross_prod(v[1], A, B) > 0;
}

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

void convex_hull()
{
    sort_points();

    v[0] = v[n];
    head = 1;
    for (int i = 2; i <= n; ++i) {
        while (cross_prod(v[head - 1], v[head], v[i]) <= 0) {
            if (head > 1)
                --head;
            else if (i == n)
                break;
            else ++i;
        }
        ++head;
        swap(v[head], v[i]);
    }
}

void read()
{
    ifstream fin("infasuratoare.in");
    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> v[i].x >> v[i].y;
}

void write()
{
    ofstream fout("infasuratoare.out");
    fout << head << endl;
    fout << fixed;
    for (int i = head; i >= 1; --i)
        fout << setprecision(6) << v[i].x << " " << v[i].y << endl;
}

int main()
{
    read();
    convex_hull();
    write();

    return 0;
}