Cod sursa(job #1378102)

Utilizator gapdanPopescu George gapdan Data 6 martie 2015 10:32:37
Problema Infasuratoare convexa Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <algorithm>
#include <cstdio>
#define NMAX 120005

using namespace std;
int n,k;

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

double panta(punct a,punct b,punct c)
{
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

bool cmp(punct p,punct r)
{
    return panta(v[1],p,r) < 0;
}

int main()
{
    ifstream f("infasuratoare.in");
    freopen("infasuratoare.out","w",stdout);

    f>>n;
    for(int i = 1; i <= n; ++i)
        f>>v[i].x>>v[i].y;

    int poz = 1, yMin=v[1].y, xMin=v[1].x;
    for(int i = 2; i <= n; ++i)
    {
        if (v[i].y < yMin) yMin=v[i].y,poz=yMin;
            else if (v[i].y == yMin && v[i].x < xMin) xMin = v[i].x,poz=i;
    }

    swap(v[1],v[poz]);
    sort(v+2,v+n+1,cmp);

    k=2;st[1]=v[1];st[2]=v[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];
    }

    printf("%d\n",k);
    for(int i = k; i >= 1; --i)
    {
        printf("%.9lf %.9lf\n",st[i].x,st[i].y);
    }

    return 0;
}