Cod sursa(job #3239116)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 2 august 2024 14:54:14
Problema Infasuratoare convexa Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
struct Punct {
    double x, y;
} v[120002], st[120002];
int n, i, top;

static inline int Cadran(Punct p) {
    if(p.x > 0 && p.y >= 0) return 1;
    if(p.x >= 0 && p.y < 0) return 2;
    if(p.x < 0 && p.y <= 0) return 3;
    return 4;
}

static inline double Det(Punct p1, Punct p2, Punct p3) {
    return ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y));
}

/// face pana la 0
/// nu returneaza sqrt
static inline double Dist(Punct p) {
    return ((p.x * p.x) + (p.y * p.y));
}

static inline bool Cmp(Punct a, Punct b) {
    int ca = Cadran(a);
    int cb = Cadran(b);

    if(ca < cb) return false;
    else if(ca == cb) {
        double d = Det({0, 0}, a, b);
        if(d != 0) return (d > 0);
        return (Dist(a) > Dist(b));
    }
}

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

    sort(v + 1, v + n + 1, Cmp);

    v[n + 1] = v[1];

    st[++top] = v[1];
    for(i = 2; i <= n + 1; i++) {
        while(top >= 2 && Det(st[top - 1], st[top], v[i]) < 0) top--;
        st[++top] = v[i];
    }

    fout << top - 1 << "\n";
    fout << fixed << setprecision(6);
    for(i = 1; i < top; i++) fout << st[i].x << " " << st[i].y << "\n";

    return 0;
}