Cod sursa(job #1737515)

Utilizator cristina_borzaCristina Borza cristina_borza Data 4 august 2016 12:51:03
Problema NextSeq Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <algorithm>
#include <fstream>
#include <cstring>
#include <set>

#define se second
#define fi first

#define DIM 100005

using namespace std;

ifstream f ("nextseq.in");
ofstream g ("nextseq.out");

int fn[DIM] , sn[DIM] , unu[DIM] , v[DIM] , qq[DIM];
int n , m , p , x , nr , BASE;

int a[DIM] , b[DIM];

bool comp (int a[] , int b[]) {
    if (a[0] != b[0]) {
        return 0;
    }

    for (int i = 1; i <= a[0]; ++i) {
        if (a[i] != b[i]) {
            return 0;
        }
    }

    return 1;
}

void adun (int a[] , int b[]);

int caut_bin(int val) {
    int i , p = 0;
    for (i = 1; i <= n; i <<= 1);
    while (i) {
        if (i + p <= n && a[i + p] <= val) {
            p += i;
        }

        i >>= 1;
    }

    return p;
}

int main() {
    unu[1] = unu[0] = 1;
    f >> n >> m >> p;

    for (int i = 1; i <= n; ++i) {
        f >> a[i];
        b[i] = a[i];
    }

    sort (a + 1 , a + n + 1);
    for (int i = 1; i <= n; ++i) {
        v[b[i]] = caut_bin(b[i]);
    }

    BASE = n + 1;
    for (int i = m; i >= 1; --i) {
        f >> x;
        fn[i] = v[x];
    }

    fn[0] = m;
    for (int i = p; i >= 1; --i) {
        f >> x;
        sn[i] = v[x];
    }

    sn[0] = p;
    adun(fn , unu);

    while (!comp(fn , sn)) {
        adun(fn , unu);
    }

    g << nr - 1;
    return 0;
}

void adun (int a[] , int b[]) {
    int t = 0 , ok = 1;
    a[0] = max(a[0] , b[0]);
    for (int i = 1; i <= a[0]; ++i) {
        a[i] = a[i] + b[i] +t;
        t = a[i] / BASE;
        a[i] %= BASE;

        if (a[i] == 0) {
            ok = 0;
        }
    }

    while (t) {
        a[++a[0]] = t % BASE;
        t /= BASE;
    }

    if (ok)
        ++nr;
}