Cod sursa(job #2208788)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 31 mai 2018 16:15:18
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");

int n, i;
int vf, poz, ok;

struct punct {
    double x, y;
};

punct a[120005];

int stiva[120005];
bool pus[120005];

bool cmp (punct a, punct b)
{
    if(a.x > b.x)
        return false;
    if(a.x == b.x && a.y > b.y)
        return false;
    return true;
}

double determinant (punct a, punct b, punct c)
{
    return ((a.x*b.y+b.x*c.y+c.x*a.y)-(b.y*c.x+c.y*a.x+a.y*b.x));
}

int main()
{
    f>>n;

    for(i=1; i<=n; i++)
        f>>a[i].x>>a[i].y;

    sort(a+1, a+n+1, cmp);

    stiva[1]=1;
    stiva[2]=2;

    pus[2]=true;

    vf=2;
    poz=3;
    ok=1;

    while(!pus[1]) {

        while(pus[poz]) {

            if(poz==n)
                ok=-1;

            poz+=ok;
        }

        while( vf>=2 && determinant (a[stiva[vf-1]], a[stiva[vf]], a[poz])<0 ) {
            pus[stiva[vf]]=false;
            vf--;
        }

        vf++;
        stiva[vf]=poz;

        pus[poz]=true;
    }

    g<<vf-1<<'\n';

    g<<setprecision(6)<<fixed;

    for(i=2; i<=vf; i++)
        g<<a[stiva[i]].x<<' '<<a[stiva[i]].y<<'\n';

    return 0;
}