Cod sursa(job #1970046)

Utilizator FrequeAlex Iordachescu Freque Data 18 aprilie 2017 20:28:04
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>

using namespace std;

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

struct Point
{
    double x;
    double y;

    Point(double _x = 0, double _y = 0)
    {
        x = _x;
        y = _y;
    }

    bool operator< (const Point & arg)const
    {
        if (x != arg.x)
            return x < arg.x;
        return y < arg.y;
    }
};

const int NMAX = 120000 + 5;

int n, head;
Point v[NMAX], s[NMAX];


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

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

bool cmp(Point A, Point B)
{
    return cross_product(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, cmp);
}

void convex_hull()
{
    sort_points();
    s[1] = v[1];
    s[2] = v[2];
    head = 2;
    for (int i = 3; i <= n; ++i)
    {
        while (head >= 2 && cross_product(s[head - 1], s[head], v[i]) > 0)
            --head;
        s[++head] = v[i];
    }
}

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

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