Cod sursa(job #2210455)

Utilizator bebeetarepredescu bebeetare Data 6 iunie 2018 19:31:35
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
int n,i;
int nr,poz,ok;
struct ceva
{
    double x,y;
};
ceva a[120005];
int st[120005];
bool p[120005];
bool cmp(ceva a,ceva b)
{
    if(a.x>b.x)
            return false;
    if(a.x==b.x && a.y>b.y)
            return false;
    return true;
}
double det(ceva a,ceva b,ceva 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);
    st[1]=1;
    st[2]=2;
    p[2]=true;
    nr=2;
    poz=3;
    ok=1;
    while(!p[1])
    {
        while(p[poz])
        {
            if(poz==n)
                ok=-1;
            poz+=ok;
        }
        while(nr>=2 && det(a[st[nr-1]],a[st[nr]],a[poz])<0)
        {
            p[st[nr]]=false;
            nr--;
        }
        nr++;
        st[nr]=poz;
        p[poz]=true;
    }
    g<<setprecision(6)<<fixed;
    g<<nr-1<<'\n';
    for(i=2; i<=nr; i++)
        g<<a[st[i]].x<<' '<<a[st[i]].y<<'\n';
    return 0;
}