Cod sursa(job #1777958)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 13 octombrie 2016 09:01:08
Problema NextSeq Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <cstdio>

#define MAXN 10000
#define MAXX 10000

int fr[MAXX+1], v[MAXN+2], u[MAXN+2], n, a, b;

inline void add(){
    v[0]++;
    int i=0;
    while((i<a)&&(v[i]==n)){
        v[i]=0;
        i++;
        v[i]++;
    }
    if(i==a){
        a++;
        for(int j=0; j<a; j++)
            v[j]=0;
    }
}

inline bool smaller(){
    if(a<b) return 1;
    int i=a-1;
    while((i>0)&&(v[i]==u[i]))
        i--;
    return v[i]<u[i];
}

int main(){
    int x, ans;
    FILE *fin, *fout;
    fin=fopen("nextseq.in", "r");
    fout=fopen("nextseq.out", "w");
    fscanf(fin, "%d%d%d", &n, &a, &b);
    for(int i=0; i<n; i++){
        fscanf(fin, "%d", &x);
        fr[x]++;
    }
    for(int i=1; i<=MAXX; i++)
        fr[i]+=fr[i-1];
    for(int i=0; i<a; i++){
        fscanf(fin, "%d", &x);
        v[a-i-1]=fr[x]-1;
    }
    for(int i=0; i<b; i++){
        fscanf(fin, "%d", &x);
        u[b-i-1]=fr[x]-1;
    }
    add();
    ans=0;
    while(smaller()){
        ans++;
        add();
    }
    fprintf(fout, "%d\n", ans);
    fclose(fin);
    fclose(fout);
    return 0;
}