Cod sursa(job #2044388)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 21 octombrie 2017 09:48:06
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.86 kb
#include <bits/stdc++.h>
#define nmax 120007
using namespace std;

struct Punct
{
    double x, y;
};
Punct a[nmax];
int n;
int st[nmax], top;
int v[nmax];
/// v[i] = 1 daca a[i]

///ret <0 daca punctul a[p] este in
///semiplanul dreptei de punctele (a[i], a[j])
double F(int i, int j, int p)
/// a[i]-A
/// a[j]-B
/// a[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;
}

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();
}

inline bool Compara(const Punct A, const Punct 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, Compara);

    st[++top] = 1;
    st[++top] = 2;
    v[1] = 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;
    }
     v[1]=0;
    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;
    ofstream fout ("infasuratoare.out");

    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()
{
    /*
    /// test
    a[1].x = a[1].y = 0; /// Originea
    a[2].x = a[2].y = 6;
    a[3].x = 7; a[3].y = 7;

    cout << F(1, 2, 3);
    */

    Citire();
    Hill();
    Afisare();
    return 0;
}