Cod sursa(job #731548)

Utilizator wefgefAndrei Grigorean wefgef Data 8 aprilie 2012 13:38:08
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <algorithm>
#include <iomanip>
#include <fstream>
using namespace std;

#define x first
#define y second

typedef pair<double, double> Point;

const int MAX_N = 120005;
const double EPS = 1e-12;

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

int n;
Point v[MAX_N];
bool vis[MAX_N];
int st[MAX_N], head;

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

double cross_product(Point O, Point A, Point B) {
    return (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y);
}

void convex_hull() {
    sort(v + 1, v + n + 1);

    st[1] = 1; st[2] = 2; head = 2;
    vis[2] = true;

    for (int i = 1, p = 1; i > 0; i += (p = (i == n ? -p : p))) {
        if (vis[i]) continue;
        while (head >= 2 && cross_product(v[st[head - 1]], v[st[head]], v[i]) < EPS)
            vis[st[head--]] = false;
        st[++head] = i;
        vis[i] = true;
    }

    // write
    fout << head - 1 << "\n";
    fout << setprecision(6) << fixed;
    for (int i = 1; i < head; ++i)
        fout << v[st[i]].x << " " << v[st[i]].y << "\n";
}

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