Pagini recente » Cod sursa (job #643526) | Cod sursa (job #1672890) | Cod sursa (job #2510936) | Cod sursa (job #911397) | Cod sursa (job #885404)
Cod sursa(job #885404)
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define nmax 120010
#define x first
#define y second
#define dd pair<double, double>
using namespace std;
const char iname[] = "infasuratoare.in";
const char oname[] = "infasuratoare.out";
dd A[nmax], st[nmax];
int r, N, i;
struct cmp
{
bool operator () (const dd &A, const dd &B) const
{
if(A.y < B.y) return 1;
if(A.y > B.y) return 0;
if(A.x < B.x) return 1;
return 0;
};
};
inline int semnDet(dd a, dd b, dd c)
{
double A = a.y - b.y, B = b.x - a.x, C = a.x * b.y - a.y * b.x;
return (A * c.x + B * c.y + C) < 0;
}
void ConvexHull()
{
st[++r] = A[1];
st[++r] = A[2];
for(i = 3; i <= N; ++i)
{
while(r >= 2 && semnDet(st[r - 1], st[r], A[i]))
--r;
st[++r] = A[i];
}
for(i = N - 1; i; --i)
{
while(r >= 2 && semnDet(st[r - 1], st[r], A[i]))
--r;
st[++r] = A[i];
}
--r;
printf("%d\n", r);
for(i = 1; i <= r; ++i) printf("%lf %lf\n", st[i].x, st[i].y);
}
int main()
{
freopen(iname, "r", stdin);
freopen(oname, "w", stdout);
scanf("%d", &N);
for(i = 1; i <= N; ++i) scanf("%lf %lf", &A[i].x, &A[i].y);
sort(A + 1, A + N + 1, cmp());
ConvexHull();
return 0;
}