Cod sursa(job #1029331)

Utilizator R4DIC4LTeodorescu Oana Maria R4DIC4L Data 15 noiembrie 2013 13:01:42
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
#define NMAX 120000
#define x first
#define y second

typedef pair<double, double> Point;

const double EPS = 1e-12;

ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");

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

void read()
{
    f >> n;
    for (int i = 1; i <= n; ++i )
        f >> 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()
{
    /// sorteaza vectorul de puncte
    sort(v + 1, v + n + 1);
    /// initializeaza stiva
    st[1] = 1; st[2] = 2; head = 2;
    /// marcheaza punctul ca vizitat
    vis[2] = true;
    /// parcurge crescator lista de puncte, apoi descrescator
    for (int i = 1, p = 1; i > 0; i += (p = (i == n ? -p : p) ) )
    {
        /// daca punctul i este vizitat, se continua cautarea
        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;
    }

    /// print number of points in convex hull
    g << head - 1 << "\n";
    g << setprecision(6) << fixed;
    /// print convex hull points' coordinates
    for (int i = 1; i < head; ++i)
        g << v[st[i]].x << " " << v[st[i]].y << "\n";
}

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