Cod sursa(job #1607059)

Utilizator Bot32King Max Bot32 Data 20 februarie 2016 19:56:21
Problema Infasuratoare convexa Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <cstdio>
using namespace std;

#define x first
#define y second
#define pb push_back
#define mp make_pair
#define point pair < float , float >

ifstream f("infasuratoare.in");
//ofstream g("infasuratoare.out");

vector < point > v;
point st[120001];
int n , top ;
float xx , yy;

int ecuatia_dreptei( point a , point b , point c )
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}

bool comp ( const point a , const point b )
{
    return ecuatia_dreptei(v[0],a,b)<0;
}

void citire_date()
{
    f >> n ;
    for ( ; n-- ; )
    {
        f >> xx >> yy;
        v.pb(mp(xx,yy));
    }
    n = v.size();
}

void sortare_puncte()
{
    int poz = 0;
    for ( int i = 1; i < n ; i++ )
        if ( v[i] < v[poz] )
            poz = i;
    swap(v[0],v[poz]);
    sort(v.begin()+1 , v.end() , comp );
}

void afis ( )
{
    freopen("infasuratoare.out","w",stdout);
    for ( int i = top ; i >= 1; i-- )
        printf("%.6f %.6f\n",st[i].x,st[i].y);
}

void infasuratoare_convexa()
{   top = 2;
    st[1] = v[0];
    st[2] = v[1];
    for ( int i = 2 ; i < n ; i++ )
    {
        while ( top >= 2 and ecuatia_dreptei(st[top-1],st[top],v[i]) > 0 ) top--;
        st[++top] = v[i];
    }
}

int main()
{
    citire_date();
    sortare_puncte();
    infasuratoare_convexa();
    afis();
    return 0;
}