Cod sursa(job #969081)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 3 iulie 2013 14:31:15
Problema Infasuratoare convexa Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.64 kb

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <utility>
#include <vector>

typedef std::pair<double,double> Point;
typedef std::vector<Point> Polygon;

const int MAX_SIZE(120001);

Polygon Hull;
std::vector<Point> Points;
Point Stack [MAX_SIZE];

inline void Read (void)
{
	std::freopen("infasuratoare.in","r",stdin);
	double x, y;
	int n;
	std::scanf("%d",&n);
	for (int counter(0) ; counter < n ; ++counter)
	{
		std::scanf("%lf %lf",&x,&y);
		Points.push_back(std::make_pair(x,y));
	}
	std::fclose(stdin);
}

inline void Print (void)
{
	std::freopen("infasuratoare.out","w",stdout);
	std::printf("%lu\n",Hull.size());
	for (int index(0), end(Hull.size()) ; index < end ; ++index)
		std::printf("%.9lf %.9lf\n",Hull[index].first,Hull[index].second);
	std::fclose(stdout);
}

inline double Determinant (Point &a, Point &b, Point &c)
{
	return a.first * b.second - a.second * b.first + b.first * c.second - b.second * c.first + c.first * a.second - c.second * a.first;
}

inline void Graham (void)
{
	const int END(Points.size());
	int index;
	for (index = 0 ; index < END ; ++index)
		if (Points[index] < Points[0])
			std::swap(Points[0],Points[index]);
	std::sort(Points.begin() + 1,Points.end(), [ ] (Point a, Point b) -> bool {return std::atan2(a.second,a.first) > std::atan2(b.second,b.first);});
	Stack[0] = Points[0];
	Stack[1] = Points[1];
	int top(1);
	for (index = 2 ; index < END ; ++index)
	{
		while (top >= 1 && Determinant(Stack[top - 1],Stack[top],Points[index]) > 0)
			--top;
		Stack[++top] = Points[index];
	}
	while (top >= 0)
	{
		Hull.push_back(Stack[top]);
		--top;
	}
}

int main (void)
{
	Read();
	Graham();
	Print();
	return 0;
}