Cod sursa(job #1619280)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 28 februarie 2016 14:44:00
Problema Infasuratoare convexa Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct punct{
float x,y;
};
punct v[120005];
float panta(punct a,punct b,punct c)
{
    return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
bool compar(punct a,punct b)
{
    return panta(v[1],a,b)>0;
}
int n,poz,st[120005],vf;
int main()
{
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);
    scanf("%d",&n);
    poz=1;
    for (int i=1;i<=n;i++)
    {
        scanf("%f %f",&v[i].x,&v[i].y);
        if (v[i].x<v[poz].x || v[i].x==v[poz].x && v[i].y<v[poz].y)
        {
            poz=i;
        }
    }
    swap(v[1],v[poz]);
    sort(v+2,v+n+1,compar);
    st[++vf]=1;
    st[++vf]=2;
    for (int i=3;i<=n;i++)
    {
        while (vf>=2 && panta(v[st[vf-1]],v[st[vf]],v[i])<0) vf--;
        st[++vf]=i;
    }
    printf("%d\n",vf);
    for (int i=vf;i>=1;i--)
        printf("%f %f\n",v[st[i]].x,v[st[i]].y);
    return 0;
}