Cod sursa(job #2173480)

Utilizator andreigeorge08Sandu Ciorba andreigeorge08 Data 15 martie 2018 22:26:31
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
int n, viz[120005], st[120005];

struct db{
    double x, y;
}a[120005];

void Citire()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> a[i].x >> a[i].y;
}

inline bool Cmp(const db A, const db B)
{
    if(A.y == B.y)
        return A.x < B.x;
    return A.y < B.y;
}

inline double F(int i, int j, int p)
{
    return a[p].x * (a[i].y - a[j].y) +
            a[p].y * (a[j].x - a[i].x) +
            a[i].x * a[j].y - a[i].y * a[j].x;
}

void Rezolvare()
{

    sort(a + 1, a + n + 1, Cmp);
    int top;

    st[1] = 1;
    viz[2] = 1;
    st[2] = 2;
    top = 2;

    for(int i = 3; i <= n; i++)
    {
        while(top > 1 && F(st[top - 1], st[top], i) < 0)
        {
            viz[st[top]] = 0;
            top--;
        }
        viz[i] = 1;
        st[++top] = i;
    }

    for(int i = n - 1; i >= 1; i--)
        if(!viz[i])
        {
            while(top > 1 && F(st[top - 1], st[top], i) < 0)
            {
                viz[st[top]] = 0;
                top--;
            }
            viz[i] = 1;
            st[++top] = i;
        }
    fout << top - 1 << "\n";

    for(int i = 1; i < top; i++)
        fout << fixed << setprecision(13) << a[st[i]].x << " "
            << a[st[i]].y << "\n";
}
int main()
{
    Citire();
    Rezolvare();
    return 0;
}