Cod sursa(job #1412402)

Utilizator gapdanPopescu George gapdan Data 1 aprilie 2015 11:53:08
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <iomanip>
#define NMAX 120005

using namespace std;
int n,k;

struct punct
{
    double x,y;
}v[NMAX],st[NMAX];

int panta(punct a, punct b, punct c)
{
    return ((b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y));
}
bool cmp(punct a, punct b)
{
    return (panta(v[1],a,b)<0);
}

int main()
{
    ifstream f("infasuratoare.in");
    ofstream g("infasuratoare.out");
    f>>n;
    f>>v[1].x>>v[1].y;
    punct p = v[1];
    int poz = 1;
    for(int i = 2; i <= n; ++i)
    {
        f>>v[i].x>>v[i].y;
        if (v[i].y < p.y) p = v[i],poz=i;
            else if (v[i].y == p.y && v[i].x < p.x) p = v[i],poz=i;
    }
    swap(v[1],v[poz]);
    sort(v+2,v+n+1,cmp);
    st[1] = v[1]; st[2] = v[2];
    k = 2;
    for(int i = 3; i <= n; ++i)
    {
        while( k >= 2 && panta(st[k-1],st[k],v[i]) > 0) --k;
        st[++k]=v[i];
    }
    g<<k<<"\n";
    for(int i = k; i >= 1; --i)
    {
        g << setprecision(9) << fixed<< st[i].x<<" ";
        g << setprecision(9) << fixed<< st[i].y<<"\n";

    }
    return 0;
}