Pagini recente » Cod sursa (job #882938) | Cod sursa (job #2030337) | Cod sursa (job #979232) | Cod sursa (job #161472) | Cod sursa (job #1858010)
#include <cstdio>
#include <algorithm>
#define N 120001
using namespace std;
struct point{
double x,y;
}V[N],S[N];
int n,top;
void Read()
{
freopen("infasuratoare.in","r",stdin);
scanf("%d",&n);
for (int i=1;i<=n;++i)
scanf("%lf%lf",&V[i].x,&V[i].y);
}
inline double CrossProduct(point A,point B,point C)
{
return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);
}
inline bool cmp(point p1,point p2)
{
return CrossProduct(V[1],p1,p2)<0;
}
void Sort()
{
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 ConvexHull()
{
Sort();
S[1]=V[1];S[2]=V[2];
top=2;
for (int i=3;i<=n;++i)
{
while (top>=2 && CrossProduct(S[top-1],S[top],V[i])>0) --top;
S[++top]=V[i];
}
}
void Write()
{
freopen("infasuratoare.out","w",stdout);
printf("%d\n",top);
for (int i=top;i>0;--i)
printf("%0.6lf %0.6lf\n",S[i].x,S[i].y);
}
int main()
{
Read();
ConvexHull();
Write();
return 0;
}
/*#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iomanip>
#define x first
#define y second
using namespace std;
typedef pair<double, double> Point;
ifstream fin("infasuratoare.in");
ofstream fout("infasuratoare.out");
const int MAX_N = 120005;
int n;
Point v[MAX_N];
Point stack[MAX_N];
int head;
void read() {
fin >> n;
for (int i = 1; i <= n; ++i)
fin >> v[i].x >> v[i].y;
}
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 int 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] < v[pos]) pos = i;
swap(v[1], v[pos]);
sort(v + 2, v + n + 1, cmp);
}
void convex_hull() {
sort_points();
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];
}
}
void write() {
fout << fixed;
fout << head << "\n";
for (int i = head; i >= 1; --i)
fout << setprecision(9) << stack[i].x << " " << stack[i].y << "\n";
}
int main() {
read();
convex_hull();
write();
}*/