Cod sursa(job #1063370)

Utilizator FlameingoAiordachioaei Marius Flameingo Data 21 decembrie 2013 11:44:18
Problema Gradina Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.58 kb
#include <cstdio>
#include <algorithm>
#include <list>
#include <bitset>
#define x first.first
#define y first.second
#define index second
using namespace std;

typedef pair <pair <int, int>, int> point;
const long long INFI = 2e18;
const int NMAX = 1003;

bitset <NMAX> in_stk;
point P[NMAX];
int stk[NMAX], head, L;

inline bool cross_product (const point &a, const point &b, const point &c) {

    return (a.x - c.x) * 1LL * (b.y - c.y) -  (a.y - c.y) * 1LL * (b.x - c.x) > 0;

}

inline long long square_distance (const point &a, const point &b) {

    return (a.x - b.x) * 1LL * (a.x - b.x) + (a.y - b.y) * 1LL * (a.y - b.y);

}

void convex_hull () {

    /// oprire
    int i, s, start_point = 1, id = 1, k = 1;
    point last;
    bool trig = 1;
    while (L >= 3) {
        in_stk.reset ();
        stk[1] = 1; stk[2] = head = 2;
        in_stk[2] = 1;
        s = 0;
        for (i = 3; i <= L; ++i) {
            while (cross_product (P[stk[head - 1]], P[stk[head]], P[i]) != trig && head >= 2)
                in_stk[stk[head--]] = 0;
            stk[++head] = i;
            in_stk[i] = 1;
        }
        for (i = L - 1; i >= 1; --i)
            if (!in_stk[i]) {
                while (cross_product (P[stk[head - 1]], P[stk[head]], P[i]) != trig && head >= 2)
                    in_stk[stk[head--]] = 0;
                stk[++head] = i;
                in_stk[i] = 1;
            }
        for (i = k; i < head; ++i)
            printf ("%d ", P[stk[i]].index);
        for (i = 1; i < k; ++i)
            printf ("%d ", P[stk[i]].index);
        last = P[stk[k - 1]];
        for (i = 1; i <= L; ++i)
            if (!in_stk[i])
                P[++s] = P[i];
        if (trig) {
            if (P[1].y > P[2].y)
                k = 1;
            else
                k = 2;
        }
        else
            if (P[1].y > P[2].y)
                k = 2;
            else
                k = 1;
        L = s;
        trig = !trig;
    }
    if (L == 1) {
        printf ("%d", P[1].index);
    }
    else
        if (L == 2)
            if (cross_product (P[1], P[2], last) == trig)
                printf ("%d %d", P[1].index, P[2].index);
            else
                printf ("%d %d", P[2].index, P[1].index);

}

int main () {

    freopen ("mission.in", "r", stdin);
    freopen ("mission.out", "w", stdout);
    int N, i;
    scanf ("%d", &N);
    for (i = 1; i <= N; ++i) {
        scanf ("%d%d", &P[i].x, &P[i].y);
        P[i].index = i - 1;
    }
    sort (P + 1, P + N + 1);
    L = N;
    convex_hull ();

}