Cod sursa(job #1379788)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 6 martie 2015 19:28:00
Problema Infasuratoare convexa Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.71 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define MAXN 120010

#ifndef ONLINE_JUDGE
ifstream in("infasuratoare.in");
//FILE *in = fopen("infasuratoare.in", "r");
FILE *out = fopen("infasuratoare.out", "w");
#endif

struct Point {
    double x;
    double y;
};

int n;
Point v[MAXN];
Point stk[MAXN];

inline double crossProd(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 comp(const Point &A, const Point &B) {
    return crossProd(v[1], A, B) < 0;
}

int main() {
    int top, pos = 1;

    in >> n;
    for(int i = 1; i <= n; ++i) {
        in >> v[i].x >> v[i].y;
        if(v[i].x < v[pos].x)
            pos = i;
    }

    swap(v[1], v[pos]);
    sort(v + 2, v + n + 1, comp);

    stk[1] = v[1];
    stk[2] = v[2];
    top = 2;
    for(int i = 3; i <= n; ++i) {
        while(top >= 2 && crossProd(stk[top - 1], stk[top], v[i]) > 0)
            top--;
        stk[++top] = v[i];
    }

    fprintf(out, "%d\n", top);
    for(int i = top; i > 0; --i) {
        fprintf(out, "%.9f %.9f\n", stk[i].x, stk[i].y);
    }

    return 0;
}