Cod sursa(job #1916478)

Utilizator GeorgianBaditaBadita Marin-Georgian GeorgianBadita Data 9 martie 2017 09:35:09
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#include <algorithm>
#include <iomanip>
#define NMAX 120001
#define EPS 1e-12
using namespace std;

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

struct Point {
    double x, y;
};

Point v[NMAX];
int stiva[NMAX];
bool viz[NMAX];
int N, head;

void readData() {
    f >> N;
    for(int i = 1; i<=N; i++)
        f >> v[i].x >> v[i].y;
}
bool cmp(Point A, Point B) {
    if(A.x < B.x)
        return true;
    if(A.x == B.x)
        return A.y < B.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() {
    sort(v + 1, v + N + 1, cmp);

    stiva[1] = 1; stiva[2] = 2; head = 2;
    viz[2] = true;
    for(int i = 1, p = 1; i> 0; i+= (p = (i == N ? -p : p))) {
        if(viz[i]) continue;
        while(head >= 2 && cross_product(v[stiva[head - 1]], v[stiva[head]], v[i]) < EPS)
            viz[stiva[head --]] = false;
        stiva[++head] = i;
        viz[i] = true;
    }
}
void writeData() {
    g << head - 1 << '\n';
    g << setprecision(6) << fixed;
    for(int i = 1; i<head; i++)
        g << v[stiva[i]].x << ' ' << v[stiva[i]].y << '\n';
}
int main() {
    readData();
    convex_hull();
    writeData();
    return 0;
}