Cod sursa(job #2365835)

Utilizator calinfloreaCalin Florea calinflorea Data 4 martie 2019 16:45:51
Problema Infasuratoare convexa Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <bits/stdc++.h>
#define nmax 120005
using namespace std;

ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
int n, top;
int st[nmax];
bool v[nmax];///1 daca e pe stiva
struct DB
{
    double x, y;
};

DB a[nmax];
void Citire()
{
    int i;

    fin >> n;

    for(i = 1; i <= n; i++)
        fin >> a[i].x >> a[i].y;
    fin.close();
}

///returneaza <0 daca a[p] este in semiplanul - al dr.
///(a[i], a[j])
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[j].x * a[i].y;
}

inline bool Compar(const DB A, const DB B)
{
    if(A.y == B.y)
        return A.x < B.x;
    return A.y < B.y;
}
///algoritmul lui Hill
void Hill()
{
    int i;
    sort(a + 1, a + n + 1, Compar);

    st[++top] = 1;
    st[++top] = 2;
    v[2] = 1;

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

    for(i = n - 1; i >= 1; i--)
        if(v[i] == 0)
        {
            while(F(st[top - 1], st[top], i) < 0)
            {
                v[st[top]] = 0;
                top--;
            }
            st[++top] = i;
            v[i] = 1;
        }
}

void Afisare()
{
    int i;

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