Cod sursa(job #2808926)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 25 noiembrie 2021 17:54:05
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax=(1<<20), mod=999983;

struct str{
    int nr, card;
};

vector <str> hx[mod], hy[mod];

int v[nmax+1];

int cautare(int x, vector <str> *h){
    int a=x%mod;
    for(int i=0;i<int(h[a].size());i++){
        if(h[a][i].nr==x){
            return i;
        }
    }
    return -1;
}

str h_str(int x, int y){
    str sol;
    sol.nr=x;
    sol.card=y;
    return sol;
}

int main(){
    int n,l,u;
    fin>>n>>l>>u;
    for(int i=1;i<=n;i++){
        fin>>v[i];
    }
    int x=1,aux=0,y=1,auy=0, sol=0;
    for(int i=1;i<=n;i++){
        while(x<=n&&aux<u+1){
            if(cautare(v[x],hx)==-1){
                hx[v[x]%mod].push_back(h_str(v[x],1));
                aux++;
            }else{
                int a=cautare(v[x],hx);
                hx[v[x]%mod][a].card++;
            }
            x++;
        }
        while(y<=n&&auy<l){
            if(cautare(v[y],hy)==-1){
                hy[v[y]%mod].push_back(h_str(v[y],1));
                auy++;
            }else{
                int a=cautare(v[y],hy);
                hy[v[y]%mod][a].card++;
            }
            y++;
        }
        if ( y <= n && auy >= l ) {
            sol += x-y+1;
        }
        int b=v[i]%mod;
        int a=cautare(v[i],hx);
        hx[b][a].card--;
        if(hx[b][a].card==0){
            hx[b][a]=hx[b].back();
            hx[b].pop_back();
            aux--;
        }
        a=cautare(v[i],hy);
        hy[b][a].card--;
        if(hy[b][a].card==0){
            hy[b][a]=hy[b].back();
            hy[b].pop_back();
            auy--;
        }
    }
    fout<<sol<<"\n";
    return 0;
}