Pagini recente » Cod sursa (job #201952) | Rating Mocanu Radu (Radu_Mocanu) | Cod sursa (job #579920) | Cod sursa (job #1847744) | Cod sursa (job #969078)
Cod sursa(job #969078)
#include <cstdio>
#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 Determinant(Points[0],a,b) < 0;});
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];
}
Hull.push_back(Stack[0]);
while (top > 0)
{
Hull.push_back(Stack[top]);
--top;
}
}
int main (void)
{
Read();
Graham();
Print();
return 0;
}