Cod sursa(job #1121043)

Utilizator StefansebiStefan Sebastian Stefansebi Data 25 februarie 2014 11:16:57
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iomanip>

using namespace std;

ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");

struct punct {
    double x; double y;
};

int n;
punct v[120005];

punct stack[120005];
int head;

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

inline double prod(punct A, punct B, punct C) {
    return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}

inline int cmp(punct p1, punct p2) {
    return prod(v[1], p1, p2) < 0;
}

void sort_points() {
    int pos = 1;
    for (int i = 2; i <= n; ++i)
        if (v[i].x < v[pos].x)
            pos = i;
        else if (v[i].x == v[pos].x and v[i].y < v[pos].y)
            pos = i;
    swap(v[1], v[pos]);
    sort(v + 2, v + n + 1, cmp);
}

void convex_hull() {
    sort_points();

    stack[1] = v[1];
    stack[2] = v[2];
    head = 2;
    for (int i = 3; i <= n; ++i) {
        while (head >= 2 && prod(stack[head - 1], stack[head], v[i]) > 0)
            --head;
        stack[++head] = v[i];
    }
}

void write() {
    fout << fixed;
    fout << head << "\n";
    for (int i = head; i >= 1; --i)
        fout << setprecision(9) << stack[i].x << " " << stack[i].y << "\n";
}

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