Cod sursa(job #3238498)

Utilizator BurloiEmilAndreiBurloi Emil Andrei BurloiEmilAndrei Data 25 iulie 2024 20:23:30
Problema NextSeq Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define pb push_back

const string FILE_NAME = "nextseq";
const int MAX_N = 1e4, MAX_M = 1e4, MAX_P = 1e4, MAX_VAL = 1e4;

int x[MAX_N + 5], a[MAX_M + 5], b[MAX_P + 5], key[MAX_VAL + 5];

int main () {
#ifndef LOCAL
    ifstream cin(FILE_NAME + ".in");
    ofstream cout(FILE_NAME + ".out");
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, p, i, pos, ans, start, end;
    bool is_equal;

    cin >> n >> m >> p;
    for (i = 1; i <= n; i++) {
        cin >> x[i];
    }
    sort(x + 1, x + n + 1);
    for (i = 1; i <= n; i++) {
        key[x[i]] = i;
    }

    start = p - m + 1;
    end = p;
    for (i = start; i <= end; i++) {
        cin >> a[i];
        a[i] = key[a[i]];
    }
    for (i = 1; i <= p; i++) {
        cin >> b[i];
        b[i] = key[b[i]];
    }

    /*
       4 2
     1 2 3
    */

    is_equal = false;
    ans = -1;
    while (!is_equal) {
        pos = p;
        while (a[pos] == n) {
            a[pos] = 1;
            pos--;
        }
        a[pos]++;

        // for (i = 1; i <= p; i++) {
        //     cout << a[i] << " ";
        // }
        // cout << "\n" << flush;

        i = 1;
        while (i <= p && a[i] == b[i]) {
            i++;
        }
        is_equal = (i > p);
        ans++;
    }

    cout << ans << "\n";
    return 0;
}