Cod sursa(job #460643)

Utilizator BitOneSAlexandru BitOne Data 3 iunie 2010 15:22:14
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.85 kb
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#define Nmax 120111

/*
 *
 */
using namespace std;
class point
{
    double x, y;
public :
    point( void ) : x(0.0), y( 0.0 ) { }
    point( double _x, double _y ) : x(_x), y(_y) { }
    inline bool operator<( const point ) const;
    bool operator>( const point& z )
    {
        return ( y > z.y || ( y == z.y && x > z.x ) );
    }
    bool cmp( const point& p, const point& p2 )
    {
        return ( x*p.y + p.x*p2.y + y*p2.x ) >= ( p.y*p2.x + p2.y*x + y*p.x );
    }
    friend inline istream& operator>>( istream& in, point& x )
    {
        in>>x.x>>x.y;
        return in;
    }
    friend inline ostream& operator<<( ostream& out, const point& x )
    {
        out<<fixed<<x.x<<' '<<x.y<<'\n';
        return out;
    }
} p, p0;
vector< point > v, ConvexHull;
inline bool point::operator<( const point z ) const
{
    return ( x-p0.x )*( z.y-p0.y ) < ( z.x-p0.x )*( y-p0.y );
}
int main( void )
{
    int N, i, j;
    ifstream in( "infasuratoare.in" );
    in>>N>>p0;
    for( i=2; i <= N; ++i )
    {
        in>>p;
        if( p0 > p )
        {
            v.push_back(p0);
            p0=p;
        }
        else v.push_back(p);
    }
    sort( v.begin(), v.end() );
    ConvexHull.push_back(p0);
    ConvexHull.push_back(v[0]);
    ConvexHull.push_back(v[1]);
    for( i=2, N-=1; i < N; ++i )
    {
        p=v[i];
        for( j=ConvexHull.size(); j >= 2 && ConvexHull[j-2].cmp( ConvexHull[j-1], p ); --j )
            ConvexHull.pop_back();
        ConvexHull.push_back(p);
    }
    ofstream out( "infasuratoare.out" );
    out<<ConvexHull.size()<<'\n';
    copy( ConvexHull.rbegin(), ConvexHull.rend(), ostream_iterator< point >( out ) );
    out<<'\n';
    return EXIT_SUCCESS;
}