Cod sursa(job #1648826)

Utilizator sorynsooSorin Soo sorynsoo Data 11 martie 2016 11:47:24
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 120005

struct pc
{
    double x,y;
}act;

vector<pc> v;
int n,i,nrst,mn;
int st[MAXN];

double determinant(pc A, pc B, pc C)
{
    return (A.x - B.x)*(B.y - C.y) - (B.x - C.x)*(A.y - B.y);
}

bool cmp(pc A, pc B)
{
    return determinant(v[0], A, B) > 0;
}

int main()
{
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);

    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        scanf("%lf %lf", &act.x, &act.y);
        v.push_back(act);
    }

    mn=0;
    for(i=1; i< v.size(); i++)
        if(v[i].x < v[mn].x || ( v[i].x == v[mn].x && v[i].y < v[mn].y))
            mn=i;

    swap(v[mn],v[0]);
    sort(v.begin()+1, v.end(), cmp);

    for(i=0; i<v.size(); i++)
    {
        while(nrst >= 2 && determinant(v[ st[ nrst-1 ] ], v[ st[ nrst ]], v[i]) < 0 )
            nrst--;

        st[ ++nrst ] = i;
    }

    printf("%d\n",nrst);
    for(i=1; i<=nrst; i++)
        printf("%lf %lf\n", v[ st[i] ].x, v[ st[i] ].y);

    return 0;
}