Cod sursa(job #1639711)

Utilizator gapdanPopescu George gapdan Data 8 martie 2016 13:37:12
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <cstdio>
#include <algorithm>
#define NMAX 120005

using namespace std;

struct punct
{
    double x,y;
}st[NMAX],v[NMAX];
int pct,i,n,k;

double det(punct a,punct b, punct c)
{
    return(a.x*b.y + b.x*c.y + c.x*a.y - b.y*c.x - c.y*a.x - a.y*b.x);
}
bool cmp(punct a,punct b)
{
    return (det(v[1],a,b) < 0);
}
int main()
{
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);
    scanf("%d",&n);scanf("%lf%lf",&v[1].x,&v[1].y);
    pct=1;
    for(i=2;i<=n;++i)
    {
        scanf("%lf%lf",&v[i].x,&v[i].y);
        if(v[i].y < v[pct].y) pct=i;
            else if(v[i].y == v[pct].y && v[i].x < v[pct].x) pct=i;
    }
    swap(v[pct],v[1]);
    sort(v+2,v+n+1,cmp);
    st[1]=v[1];st[2]=v[2];k=2;
    for(i=3;i<=n;++i)
    {
        while(k >=2 && det(st[k-1],st[k],v[i]) > 0) --k;
        ++k;
        st[k]=v[i];
    }
    printf("%d\n",k);
    for(i=k;i>=1;--i) printf("%0.6lf %0.6lf\n",st[i].x,st[i].y);
    return 0;
}