Pagini recente » Borderou de evaluare (job #1822423) | Borderou de evaluare (job #1647562) | Borderou de evaluare (job #1781022) | Borderou de evaluare (job #62006) | Cod sursa (job #1970586)
//Convex Hull
//Graham Scan
#include <bits/stdc++.h>
using namespace std;
ifstream f("infasuratoare.in");
ofstream g("infasuratoare.out");
int N;
struct Point
{
double x;
double y;
} V[120001],Stack[120001];
int head;
inline double cross_product(const Point& A, const Point& B, const Point& C) {
return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
}
inline bool cmp(const Point& p1, const Point& p2)
{
return cross_product(V[1], p1, p2) < 0;
}
void sort_points()
{
int pos = 1;
for (int i = 2; i <= N; ++i)
if (V[i].x < V[pos].x)
pos = i;
swap(V[1],V[pos]);
sort(V+2,V+N+1, cmp);
}
void convex_hull()
{
Stack[1] = V[1];
Stack[2] = V[2];
head = 2;
for (int i = 3; i <= N; ++i)
{
while (head >= 2 && cross_product(Stack[head - 1], Stack[head], V[i]) > 0) --head;
Stack[++head] = V[i];
}
}
int main()
{
f>>N;
for(int i=1; i<=N; i++)
{
double x,y;
f>>x>>y;
V[i]={x,y};
}
sort_points();
convex_hull();
g<< fixed;
g<< head << "\n";
for (int i = head; i >= 1; --i)
g << setprecision(9) << Stack[i].x << " " << Stack[i].y << "\n";
}