Cod sursa(job #2156341)

Utilizator ContDeRacistAliniateEBlat ContDeRacist Data 8 martie 2018 17:41:02
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>
#include <algorithm>
#include <iomanip>
using namespace std;

ifstream cin("infasuratoare.in");
ofstream cout("infasuratoare.out");

struct poi{
    double x, y;
    bool operator< (poi aux) {
        if (aux.y == y) {
            return x < aux.x;
        }
        return aux.y > y;
    }
    double operator* (poi aux) {
        return ((aux.y * x) - (aux.x * y)) / 2;
    }
    poi operator* (int i) {
        return (poi) {i * x, i * y};
    }
};

double ariet(poi p, poi aux1, poi aux2) {
    return p * aux1 + aux1 * aux2 - p * aux2;
}

const int N = 120021;

poi p[N];

int ic[N];

int infas(poi vec[], int siz) {
    int vf(-1);
    ic[++vf] = 0;
    ic[++vf] = 1;
    for (int i = 2; i < siz; ++i) {
        while (vf > 0 && ariet(vec[ic[vf - 1]], vec[ic[vf]], vec[i]) < 0) {
            --vf;
        }
        ic[++vf] = i;
    }
    ic[++vf] = siz - 2;
    for (int i = siz - 3; i >= 0; --i) {
        while (vf > 0 && ariet(vec[ic[vf - 1]], vec[ic[vf]], vec[i]) < 0) {
            --vf;
        }
        ic[++vf] = i;
    }
    return vf;
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> p[i].x >> p[i].y;
    }
    sort(p, p + n);
    int len(infas(p, n));
    cout << len << "\n" << fixed;
    for (int i = 0; i < len; ++i) {
        cout << setprecision(12) << p[ic[i]].x << " " << p[ic[i]].y << "\n";
    }
    return 0;
}