Cod sursa(job #450912)

Utilizator darrenRares Buhai darren Data 8 mai 2010 16:24:51
Problema Infasuratoare convexa Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 2.19 kb
#include<cmath>
#include<cstdio>
#include<fstream>
#include<iomanip>
#include<algorithm>
#include<stack>
using namespace std;

#define elif else if
const double eps = 0.0000000000001;

struct point
{
	double x, y;
};

int n, pmin;
point p[120001];
double minx = 120001, miny = 120001;
int st[120001], nr = -1;

void read();
void write();
void scan();
inline double angle( point a )
{
	if ( fabs( ( a.x - minx ) ) < eps )
		return 90;
	if ( ( a.x - minx ) > 0 )
		return 180 * ( atan( ( a.y - miny ) / ( a.x - minx ) ) / M_PI );
	else
		return 180 - fabs( 180 * ( atan( ( a.y - miny ) / ( a.x - minx ) ) / M_PI ) );
}
bool cond( const point& a, const point b )
{
	if ( fabs( angle( a ) - angle( b ) ) > eps )
		return angle( a ) < angle( b );
	return (( a.x - minx ) * ( a.x - minx ) + ( a.y - miny ) * ( a.y - miny )) - (( b.x - minx ) * ( b.x - minx ) + ( b.y - miny ) * ( b.y - miny )) > eps;
}
bool change( point x )
{
	//imi iau ultimele doua din stack
	point top = p[st[nr]];
	point pen = p[st[nr - 1]];
	//----
	/*double x1, y1, x2, y2;
	x1 = top.x - pen.x;
	y1 = top.y - pen.y;
	x2 = x.x - pen.x;
	y2 = x.y - pen.y;
	if( x1 * y2 - x2 * y1 <= 0 )*/
	if( ( top.x - pen.x ) * ( x.y - pen.y ) - ( x.x - pen.x ) * ( top.y - pen.y ) <= 0 )
        return 1;
    return 0;
}

int main()
{
	read();
	scan();
	write();
	return 0;
}

void read()
{
	freopen( "infasuratoare.in", "r", stdin );
	scanf( "%d", &n );
	for ( int i = 0; i < n; ++i )
	{
		scanf( "%lf %lf", &p[i].x, &p[i].y );
		if ( p[i].y < miny )
		{
			pmin = i;
			minx = p[i].x;
			miny = p[i].y;
		}
		elif ( ( p[i].y - miny < eps ) && p[i].x < minx )
		{
			pmin = i;
			minx = p[i].x;
		}
	}
	point aux = p[pmin];
	p[pmin] = p[0];
	p[0] = aux;
}

void write()
{
	freopen( "infasuratoare.out", "w", stdout );
	printf( "%d\n", nr + 1 );
	for ( int i = 0; i <= nr; ++i )
		printf( "%.10lf %.10lf\n", p[st[i]].x, p[st[i]].y );
}

void scan()
{
	sort( p + 1, p + n, cond );
	st[++nr] = 0;
	st[++nr] = 1;
	st[++nr] = 2;
	for ( int i = 3; i < n; ++i ) // O(n * not_ok )
	{
		while ( change( p[i] ) )
		{
			--nr;
			if ( nr == 0 )
				break;
		}
		
		st[++nr] = i;
	}
}