Cod sursa(job #2541127)

Utilizator iKnowTheAnswerun copil iKnowTheAnswer Data 8 februarie 2020 10:03:29
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <algorithm>
#include <iomanip>
#include <fstream>
using namespace std;

#define x first
#define y second

typedef pair<double, double> punct;

const int nmax = 120005;
const double EPS = 1e-12;

FILE*in;
FILE*out;

int n;
punct v[nmax];
bool vis[nmax];
int st[nmax], head;

void read() {
    fscanf(in, "%d", &n);
    for (int i = 1; i <= n; ++i)
        fscanf(in, "%lf%lf", &v[i].x, &v[i].y);
}

double cross_product(punct O, punct A, punct 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);

    st[1] = 1; st[2] = 2; head = 2;
    vis[2] = true;

    for (int i = 1, p = 1; i > 0; i += (p = (i == n ? -p : p))) {
        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;
    }
    fprintf(out, "%d\n", head-1);
    for (int i = 1; i < head; ++i)
        fprintf(out, "%.6f %.6f\n", v[st[i]].x, v[st[i]].y);
}

int main() {
    in = fopen("infasuratoare.in", "r");
    out = fopen("infasuratoare.out", "w");
    read();
    convex_hull();
}