Cod sursa(job #2333831)

Utilizator TudorCaloianCaloian Tudor-Ioan TudorCaloian Data 1 februarie 2019 23:32:34
Problema Infasuratoare convexa Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>
#define NMAX 120005
#define f first
#define s second
#define pi pair <int, int>
using namespace std;

ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");

int n, st1[NMAX], st2[NMAX], head1, head2;
pair <double, double> v[NMAX];

double det(pi a, pi b, pi c)
{
    return (b.f-a.f)*(c.s-a.s)-(c.f-a.f)*(b.s-a.s);

}

int main()
{

    fin >> n;

    for(int i = 1; i <= n; i++)
        fin >> v[i].f >> v[i].s;

    sort(v+1, v+n+1);

    st1[1] = 1;
    st1[2] = 2;
    head1 = 2;

    for(int i = 3; i <= n; i++)
    {
        while(head1 >= 2 && det(v[st1[head1-1]], v[st1[head1]], v[i]) < 0)
            head1--;
        st1[++head1] = i;
    }

    st2[1] = n;
    st2[2] = n-1;
    head2 = 2;

    for(int i = n-2; i >=1 ; i--)
    {
        while(head2 >= 2 && det(v[st2[head2-1]], v[st2[head2]], v[i]) < 0)
            head2--;
        st2[++head2] = i;
    }
    fout << head1+head2 -2<< '\n';
    fout << fixed << setprecision(12);
    for(int i = 1; i <= head1; i++)
        fout << v[st1[i]].f << " " << v[st1[i]].s << '\n';
    for(int i = 2; i < head2; i++)
        fout << v[st2[i]].f << " " << v[st2[i]].s << '\n';
    return 0;
}