Cod sursa(job #2575569)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 6 martie 2020 14:26:34
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("infasuratoare.in");
ofstream fout ("infasuratoare.out");

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

const int N = 120005;

const long double EPS = 1e-12;

struct solve
{
    long double x, y;
} a[N];

long double c_p(solve a, solve b, solve c)
{
    return (b.y - a.y) * (c.x - a.x) - (c.y - a.y) * (b.x - a.x);
}

bool cmp(solve x, solve y)
{
    return c_p(a[1], x, y) > 0;
}

int st[N];

int main()
{
    usain_bolt();

    int n, chosen = -1, k = 2;

    fin >> n;
    for(int i = 1; i <= n; ++i) {
        fin >> a[i].x >> a[i].y;
        if(chosen == -1 || a[i].x < a[chosen].x || (a[i].x == a[chosen].x && a[i].y < a[chosen].y)) chosen = i;
    }
    swap(a[1], a[chosen]);
    sort(a + 2, a + 1 + n, cmp);
    st[1] = 1;
    st[2] = 2;
    for(int i = 3; i <= n; ++i) {
        while(k >= 2 && c_p(a[st[k - 1]], a[st[k]], a[i]) < EPS) --k;
        st[++k] = i;
    }
    fout << k << "\n";
    for(int i = k; i >= 1; --i) fout << fixed << setprecision(12) << a[st[i]].x << " " << a[st[i]].y << "\n";
    return 0;
}