Cod sursa(job #1773761)

Utilizator elffikkVasile Ermicioi elffikk Data 8 octombrie 2016 10:41:56
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iomanip>

using namespace std;

struct Point {
    double x, y;
};

const int MAX_N = 120005;

int n;
vector<Point> v(MAX_N);

vector<Point> h(MAX_N);
int head;

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

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

bool cmpPoint(Point a, Point b) {
    return (a.x<b.x) || (a.x==b.x) && (a.y<b.y);
}

bool cmpAngle(Point p1, Point p2) {
    return det(v[1], p1, p2) < 0;
}

void sort_points() {
    swap(v[1], *min_element(v.begin()+1, v.begin()+n+1, cmpPoint));
    sort(v.begin() + 2, v.begin() + n + 1, cmpAngle);
}

void convex_hull() {
    sort_points();

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

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

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