Cod sursa(job #2663604)

Utilizator Senth30Denis-Florin Cringanu Senth30 Data 26 octombrie 2020 21:06:06
Problema NextSeq Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>
#define MAX 131072
#define MOD 1000000007

using namespace std;
const int NMAX = 10505;

int N, M, P, ans;
int v[NMAX], a[NMAX], b[NMAX], srt[NMAX], c[NMAX];

void read(){
    int x;
    scanf("%d%d%d", &N, &M, &P);
    for(int i = 1; i <= N; i++)
        scanf("%d", &v[i]);
    sort(v + 1, v + N + 1);
    for(int i = 1; i <= N; i++)
        srt[v[i]] = i;
    for(int i = 1 + P - M; i <= P; i++){
        scanf("%d", &x);
        a[i] = srt[x];
    }
    for(int i = 1; i <= P; i++){
        scanf("%d", &x);
        b[i] = srt[x];
    }
}

int main(){

    freopen("nextseq.in", "r", stdin);
    freopen("nextseq.out", "w", stdout);

    read();
    bool retry = true;
    while(retry){
        ans++;
        int i = P;
        while(a[i] == N){
            a[i] = 1;
            i--;
        }
        a[i]++;
        retry = false;
        for(int i = 1; i <= P && !retry; i++)
            if(a[i] != b[i]) retry = true;
    }
    ans--;
    printf("%d", ans);

    return 0;
}