Cod sursa(job #2541080)

Utilizator ikogamesIon Ceaun ikogames Data 8 februarie 2020 08:52:28
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 kb
#include <bits/stdc++.h>

using namespace std;

struct punct
{
    double x, y;
};

punct a[120005];
int n, v[120005];
int st[120005], top;

void Citire()
{
    ifstream fin("infasuratoare.in");
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> a[i].x >> a[i].y;
    fin.close();
}

inline bool Compar(punct a, punct b)
{
    if(a.y == b.y)
        return (a.x < b.x);
    return (a.y < b.y);
}

/// verifica daca punctul w se afla in semiplanul
/// + sau - al dreptei (a[i], a[j])
double F(int i, int j, punct w)
{
    return (a[i].y - a[j].y) * w.x + (a[j].x - a[i].x) * w.y +
        (a[i].x * a[j].y - a[j].x * a[i].y);
}

void Hull()
{
    punct P;
    st[1] = 1;
    st[2] = 2;
    top = 2;
    v[1] = v[2] = 1;
    for(int i = 3; i <= n; i++)
    {
        P = a[i];
        while(top >= 2 && F(st[top - 1], st[top], P) < 0)
        {
            v[st[top]] = 0;
            top--;
        }
        st[++top] = i;
        v[i] = 1;
    }
    v[1] = 0;
    for(int i = n; i >= 1; i--)
        if(!v[i])
        {
            P = a[i];
            while(top >= 2 && F(st[top - 1], st[top], P) < 0)
            {
                v[st[top]] = 0;
                top--;
            }
            st[++top] = i;
            v[i] = 1;
        }
}

void Afisare()
{
    ofstream fout("infasuratoare.out");
    top--;
    fout << top << "\n";
    fout << setprecision(12) << fixed;
    for(int i = 1; i <= top; i++)
        fout << a[st[i]].x << " " << a[st[i]].y << "\n";
    fout.close();
}

int main()
{
    Citire();
    sort(a + 1, a + n + 1, Compar);
    Hull();
    Afisare();
    return 0;
}