Cod sursa(job #1648392)

Utilizator Vlad_lsc2008Lungu Vlad Vlad_lsc2008 Data 11 martie 2016 09:52:24
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#define nmax 120003
using namespace std;

struct point
{
    double x,y;
} v[nmax],s[nmax];
int nrp,head;

inline double unghi( point a,point b,point c)
{
    return (b.y-a.y)*(c.x-a.x)-(c.y-a.y)*(b.x-a.x);
}

inline int comp( point a,point b)
{
    return unghi(v[1],a,b)>0;
}

void sort_p()
{
    int poz=1;
    for(int i=2;i<=nrp;i++)
        if( v[i].x < v[poz].x) poz=i;
        else if( v[i].x==v[poz].x && v[i].y< v[poz].y ) poz=i;
    swap(v[1],v[poz]);
    sort(v+2,v+nrp+1,comp);
}

int main()
{
    int i;
    freopen("infasuratoare.in","r",stdin);
    freopen("infasuratoare.out","w",stdout);
    scanf("%d",&nrp);
    for(i=1;i<=nrp;i++) scanf("%lf%lf",&v[i].x,&v[i].y);
    sort_p();

    s[1]=v[1]; s[2]=v[2];  head=2;
    for(i=3;i<=nrp;i++)
    {
        while( head>=2 && unghi(s[head-1],s[head],v[i])<0 )
            head--;
        s[++head]=v[i];
    }
    printf("%d\n",head);
    for(i=head;i>0;i--) printf("%lf %lf\n",s[i].x,s[i].y);

    fclose(stdin);
    fclose(stdout);
    return 0;
}