Cod sursa(job #1773775)

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

using namespace std;

struct Point {
    double x, y;
};

int n;
vector<Point> v, h;

void read() {
    ifstream fin("infasuratoare.in");
    fin >> n;
    v.resize(n);
    for (int i = 0; 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[0], p1, p2) < 0;
}

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

void convex_hull() {
    sort_points();

    h.push_back(v[0]);
    h.push_back(v[1]);
    for (int i = 2; i < v.size(); i++) {
        while (h.size() > 1 && det(*(h.end()-2), *(h.end()-1), v[i]) > 0) {
            h.erase(h.end()-1);
        }
        h.push_back(v[i]);
    }
}

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

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