Pagini recente » Cod sursa (job #356695) | Cod sursa (job #2943945) | Istoria paginii home | Cod sursa (job #303664) | Cod sursa (job #455848)
Cod sursa(job #455848)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 14, 2010, 11:02 AM
*/
#include <vector>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
#define oo 0x3f3f3f3f
/*
*
*/
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;
inline bool cmp( const point& );
inline bool cmp( const point&, const point& );
friend inline istream& operator>>( istream&, point& );
friend inline ostream& operator<<( ostream&, const point& );
} p0, p;
inline bool point::operator<( const point& z ) const
{
double xx=( x-p0.x )*( z.y-p0.y ), yy=( y-p0.y )*( z.x-p0.x );
if( xx == yy )
return x < z.x || ( x == z.x && y < z.y );
return xx < yy;
}
inline bool point::cmp( const point& z )
{
return ( y > z.y || ( y == z.y && x > z.x ) );
}
inline bool point::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 + p.x*y );
}
inline istream& operator>>( istream& in, point& z )
{
in>>z.x>>z.y;
return in;
}
inline ostream& operator<<( ostream& out, const point& z )
{
out<<fixed<<z.x<<' '<<z.y<<'\n';
return out;
}
vector< point > v, ConvexHull;
int main( void )
{
int N, i, j;
ifstream in( "infasuratoare.in" );
in>>N>>p0;
for( i=1; i < N; ++i )
{
in>>p;
if( p0.cmp(p) )
{
v.push_back(p0);
p0=p;
continue;
}
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) );
return EXIT_SUCCESS;
}