Cod sursa(job #1777933)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 13 octombrie 2016 00:13:21
Problema NextSeq Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

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

int main() {

    int valsCount, aCount, bCount;
    fin >> valsCount >> aCount >> bCount;

    vector<int> vals(valsCount);
    for (auto& it : vals)
        fin >> it;
    sort(vals.begin(), vals.end());

    vector<int> a(bCount), b(bCount);

    for (int i = 0; i < bCount; ++i) {
        if (i < bCount - aCount)
            a[i] = 0;
        else {
            fin >> a[i];
            a[i] = lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin() + 1;
        }
    }

    for (int i = 0; i < bCount; ++i) {
        fin >> b[i];
        b[i] = lower_bound(vals.begin(), vals.end(), b[i]) - vals.begin() + 1;
    }

    vector<int> pow(bCount);
    pow[0] = 1;
    for (int i = 1; i < bCount; ++i)
        pow[i] = min(100, pow[i - 1] * valsCount);

    int start = 0;
    while (start < bCount && a[start] == b[start])
        ++start;

    if (start == bCount) {
        fout << 0 << '\n';
        return 0;
    }

    int ans = (b[start] - a[start] - 1) * pow[bCount - start - 1];
    for (int curr = start + 1; curr < bCount; ++curr) {

        ans += (valsCount - a[curr]) * pow[bCount - curr - 1];
        ans += (b[curr] - 1) * pow[bCount - curr - 1];

    }

    fout << ans << '\n';

    return 0;

}

//Trust me, I'm the Doctor!