Cod sursa(job #1780073)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 15 octombrie 2016 20:33:50
Problema Gradina Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.26 kb
#include <fstream>
#include <algorithm>
#include <string>
#include <cstring>
#include <iostream>
#include <iomanip>

using namespace std;

ifstream fin ("gradina.in"); ofstream fout ("gradina.out");

const int nmax = 250;
const long long inf = (1LL << 60);
int n;
long long arie;
string s;

int ord[nmax + 1];

struct pct{
    int x, y;

    inline bool operator < (const pct &k) const {
        if (x != k.x) return (x < k.x);
        return (y < k.y);
    }
} v[nmax + 1], a[nmax + 1], b[nmax + 1], st[nmax + 1];

inline bool cmp (int x, int y) {
    return v[ x ] < v[ y ];
}

inline int ccw (pct x, pct y, pct z) {
    long long ans = (1LL * x.x * y.y - 1LL * x.y * y.x +
                     1LL * y.x * z.y - 1LL * y.y * z.x +
                     1LL * z.x * x.y - 1LL * z.y * x.x);

    if (ans > 0) return 1;
    if (ans == 0) return 0;
    return -1;
}

long long calc_arie (pct x[], int lg) {
    long long ans = 0;
    for (int i = 0; i < lg - 1; ++ i) {
        ans += (1LL * x[ i ].x * x[i + 1].y - 1LL * x[i + 1].x * x[ i ].y);
    }
    ans += (1LL * x[lg - 1].x * x[ 0 ].y - 1LL * x[ 0 ].x * x[lg - 1].y);

    if (ans < 0) ans = -ans;
    return ans;
}

bool infasuratoare (pct x[], int lg) {
    int top = 0;
    st[ ++ top ] = x[ 0 ]; st[ ++ top ] = x[ 1 ];
    for (int i = 2; i < lg; ++ i) {
        while (top >= 2 && ccw(st[top - 1], st[ top ], x[ i ]) != 1) {
            -- top;
        }
        st[ ++ top ] = x[ i ];
    }
    int dim = top;

    top = 0;
    st[(++ top) + dim] = x[lg - 1]; st[(++ top) + dim] = x[lg - 2];
    for (int i = lg - 3; i >= 0; -- i) {
        while (top >= 2 && ccw(st[dim + top - 1], st[dim + top], x[ i ]) != 1) {
            -- top;
        }
        st[(++ top) + dim] = x[ i ];
    }

    for (int i = 1; i <= dim; ++ i) {
        x[i - 1] = st[ i ];
    }
    for (int i = dim + 2; i < dim + top; ++ i) {
        x[i - 2] = st[ i ];
    }
    dim += top;

    return (dim == lg + 2);
}

void solve (int x, int y) {
    string aux;
    aux.resize( n );

    int lga, lgb;
    lga = lgb = 0;

    if (x == 2 && y == 4) {
        x = 2;
    }

    for (int i = 0; i < n; ++ i) {
        int unde = ccw(v[ x ], v[ y ], v[ ord[ i ] ]);

        if (unde > 0 || ord[ i ] == x) {
            a[ lga ++ ] = v[ ord[ i ] ];
            aux[ ord[ i ] ] = 'I';
        } else {
            b[ lgb ++ ] = v[ ord[ i ] ];
            aux[ ord[ i ] ] = 'V';
        }
    }

    if (lga < 3 || lgb < 3) return ;
    if (infasuratoare(a, lga) == 0 || infasuratoare(b, lgb) == 0) return ;
    long long dif = calc_arie(a, lga) - calc_arie(b, lgb);

    if (dif < 0) dif = -dif;

    if (aux[ 0 ] == 'V') {
        for (int i = 0; i < n; ++ i) {
            if (aux[ i ] == 'I') aux[ i ] = 'V';
            else aux[ i ] = 'I';
        }
    }

    if (dif < arie) {
        arie = dif;
        s = aux;
    } else if (dif == arie && aux < s) {
        s = aux;
    }
}

int main() {
    fin >> n;
    for (int i = 0; i < n; ++ i) {
        fin >> v[ i ].x >> v[ i ].y;
        ord[ i ] = i;
    }
    sort(ord, ord + n, cmp);

    arie = inf;
    for (int i = 0; i < n; ++ i) {
        for (int j = i + 1; j < n; ++ j) {
            solve(i, j);
        }
    }

    fout << setprecision( 1 ) << fixed;
    fout << 0.5 * arie << "\n" << s;

    fin.close();
    fout.close();
    return 0;
}