Cod sursa(job #2820347)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 20 decembrie 2021 11:38:45
Problema Infasuratoare convexa Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 kb
#include <bits/stdc++.h>
using namespace std;

/**
https://infoarena.ro/problema/infasuratoare
*/
ofstream fout("infasuratoare.out");
struct Punct
{
    double x, y;
    bool operator<(const Punct A) const
    {
        if (A.y == y) return A.x > x;
        return A.y > y;
    }
};
Punct a[120003];
int n, st[120003], top;
bitset<120003> viz;
/// viz[i] = 1 daca punctul i se afla pe stiva

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

/// avem drapta (A,B) si vream sa vedem in care
/// semiplan se afla C
double F(Punct A, Punct B, Punct C)
{
    double a, b, c;
    a = B.y - A.y;
    b = A.x - B.x;
    c = A.y * (B.x - A.x) - A.x * (B.y - A.y);
    return a * C.x + b * C.y + c;
}

void ConvexHull()
{
    int i;
    sort(a + 1, a + n + 1);
    st[1] = 1;
    st[2] = 2;
    top = 2;
    viz[2] = 1;
    for (i = 3; i <= n; i++)
    {
        /// scoatem cate un punct de pe stiva
        /// cat timp f(st[top-1], st[top], a[i])>0
        while (F(a[st[top-1]], a[st[top]], a[i]) > 0)
        {
            viz[st[top]] = 0;
            top--;
        }
        top++;
        st[top] = i;
        viz[i] = 1;
    }
    for (i = n - 1; i >= 1; i--)
        if (viz[i] == 0)
        {
            while (F(a[st[top-1]], a[st[top]], a[i]) > 0)
            {
                viz[st[top]] = 0;
                top--;
            }
            top++;
            st[top] = i;
            viz[i] = 1;
        }
}

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

int main()
{
    Citire();
    ConvexHull();
    Afisare();
    return 0;
}