Cod sursa(job #3213780)

Utilizator and_Turcu Andrei and_ Data 13 martie 2024 14:04:25
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>

using namespace std;

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

struct Punct
{
    double x, y;
    bool operator < (const Punct alt) const
    {
        if (alt.y == y)
            return x < alt.x;
        return y < alt.y;
    }
};
Punct a[120003];
int n;
bool viz[120005];
int st[120005], top;

double F(Punct A, Punct B, Punct C)
{
    return (A.y - B.y) * C.x + (B.x - A.x) * C.y
        + A.x * B.y - A.y * B.x;
}

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

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

    for (i = n; i >= 1; i--)
    {
        if (viz[i] == 0)
        {
            while (top >= 2 and F(a[st[top - 1]], a[st[top]], a[i]) <= 0)
                top--;
            st[++top] = i;
        }
    }
    fout << top - 1 << "\n";
    for (int i = 1; i < top; i++)
        fout << setprecision(12) << fixed << a[st[i]].x << " " << a[st[i]].y << "\n";


    return 0;
}