Cod sursa(job #2738918)

Utilizator stefantagaTaga Stefan stefantaga Data 6 aprilie 2021 15:42:05
Problema Infasuratoare convexa Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
struct wow
{
    long double x,y;
}v[100005];
long double determinant(wow a,wow b,wow c)
{
    return a.x*b.y+b.x*c.y+c.x*a.y-a.y*b.x-b.y*c.x-c.y*a.x;
}
bool compare (wow a,wow b)
{
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
int n,i,st[100005],ok[100005],q;
int main()
{
    f>>n;
    for (i=1;i<=n;i++)
    {
        f>>v[i].x>>v[i].y;
    }
    sort (v+1,v+n+1,compare);
    st[1]=1;
    st[2]=2;
    q=2;
    ok[1]=1;
    for (i=3;i<=n;i++)
    {
        while (q>1&&determinant(v[st[q-1]],v[st[q]],v[i])>=0)
        {
            ok[st[q]]=0;
            q--;
        }
        st[++q]=i;
        ok[i]=1;
    }
    for (i=n;i>=1;i--)
    {
        if (ok[i]==0)
        {
            while (q>1&&determinant(v[st[q-1]],v[st[q]],v[i])>=0)
        {
            ok[st[q]]=0;
            q--;
        }
        st[++q]=i;
        ok[i]=1;
        }
    }
    g<<q-1<<'\n';
    for (i=q-1;i>=1;i--)
    {
        g<<fixed<<setprecision(6)<<v[st[i]].x<<" "<<v[st[i]].y<<'\n';
    }
    return 0;
}