Cod sursa(job #2712386)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 25 februarie 2021 18:26:07
Problema NextSeq Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e4 + 4;
int N, M, P, x, v[NMAX], Hash[NMAX], a[NMAX], b[NMAX], ans;
bool ok = true;

int main() {
    fin >> N >> M >> P;
    for(int i = 1; i <= N; ++i)
        fin >> v[i];
    sort(v + 1, v + N + 1);
    for(int i = 1; i <= N; ++i)
        Hash[v[i]] = i;
    for(int i = P + 1 - M; i <= P; ++i) {
        fin >> x;
        a[i] = Hash[x];
    }
    for(int i = 1; i <= P; ++i) {
        fin >> x;
        b[i] = Hash[x];
    }
    while(ok) {
        ++ans;
        int i = P;
        while(a[i] == N)
            a[i--] = 1;
        ++a[i];
        ok = false;
        for(i = 1; i <= P && !ok; ++i)
            if(a[i] != b[i])
                ok = true;
    }
    fout << --ans << '\n';
}