Cod sursa(job #2594137)

Utilizator levladiatorDragutoiu Vlad-Ioan levladiator Data 5 aprilie 2020 14:45:24
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
#define NMAX 120005
#define x first
#define y second
using namespace std;
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");

typedef pair < double , double > point;

int n,head;

point v[NMAX],st[NMAX];

void read()
{
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        fin>>v[i].x>>v[i].y;
        if(v[i].x<v[1].x)swap(v[1],v[i]);
        else if(v[i].x==v[1].x&&v[i].y>v[1].y)swap(v[1],v[i]);
    }
}
double cross_prod(point a,point b,point c)
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
bool cmp(point a,point b)
{
    return cross_prod(v[1],a,b)<0;
}
void convex_hull()
{
    sort(v+2,v+n+1,cmp);
    st[1]=v[1];
    st[2]=v[2];
    head=2;
    for(int i=3;i<=n;i++)
    {
        while(head>=2&&cross_prod(st[head-1],st[head],v[i])>0)
            head--;

        st[++head]=v[i];
    }
}
void write()
{
    fout<<fixed;
    fout<<head<<'\n';
    for(int i=head;i>=1;i--)
    {
        fout<<setprecision(9)<<st[i].x<<" "<<st[i].y<<'\n';
    }
}

int main()
{
    read();
    convex_hull();
    write();
}