Cod sursa(job #2365821)

Utilizator calinfloreaCalin Florea calinflorea Data 4 martie 2019 16:40:58
Problema Infasuratoare convexa Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
#define NMax 120007

using namespace std;

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

struct DB
{
    double x, y;
}a[NMax];

int n, st[NMax], viz[NMax], top;

void Read ()
{
    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 int F (int p, int q, int k)
{
    return a[q].x * (a[k].y - a[p].y) +
            a[p].x * (a[q].y - a[k].y) +
            a[k].x * (a[p].y - a[q].y);
}

void Infasuratoare ()
{
    sort (a + 1, a + n + 1, CMP);

    st[1] = 1;
    st[2] = 2;
    viz[2] = 1;
    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] == 0)
        {
            while (F(st[top - 1], st[top], i) < 0)
            {
                viz[st[top]] = 0;
                top--;
            }
            viz[i] = 1;
            st[++top] = i;
        }
    }


}

void Output ()
{
    fout << top - 1 << "\n";

    for (int i = 1; i < top; i++)
        fout << fixed << setprecision(12) << a[st[i]].x << " " << a[st[i]].y << "\n";

}
int main()
{
    Read();
    Infasuratoare();
    Output();
    return 0;
}