Cod sursa(job #1034865)

Utilizator R4DIC4LTeodorescu Oana Maria R4DIC4L Data 18 noiembrie 2013 09:08:56
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
#define NMAX 120001

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].first >> v[i].second;
}

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

void convex_hull()
{
    /// sort vector of points
    sort(v + 1, v + n + 1);
    /// stack initialisation with the first 2 points
    st[1] = 1; st[2] = 2; head = 2;
    /// mark point as visited
    vis[2] = true;
    /// i increases from 1 to n and then backwards
	/// computing the convex hull using Andrew's algorithm 
    for (int i = 1, p = 1; i > 0; i += (p = (i == n ? -p : p) ) )
    {
        /// if i has been visited
        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]].first << " " << v[st[i]].second << "\n";
}

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