Cod sursa(job #2262989)

Utilizator pasoi_stefanPasoi Stefan pasoi_stefan Data 17 octombrie 2018 23:16:06
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
FILE *in=fopen("secv5.in","r");
FILE *out=fopen("secv5.out","w");
int n,s,d,a[2000005],val[2000005],nr,x,L;
long long secs,secd;
vector<int> Hash[100235];
int main(){
    fscanf(in,"%d%d%d",&n,&s,&d);
    for(int i=1;i<=n;i++){
        fscanf(in,"%d",&x);
        int key=x%100231;
        for(vector<int>::iterator it=Hash[key].begin();it!=Hash[key].end();++it)
            if(*it==x){
                a[i]=val[x];
                break;
            }
        if(!a[i]){
            Hash[key].push_back(x);
            val[x]=++nr;
            a[i]=val[x];
        }
    }
    memset(val,0,sizeof(int)*2000000);
    L=1; nr=0;
    for(int i=1;i<=n;i++){
        if(val[a[i]]==0)
            ++nr;
        ++val[a[i]];
        while(nr>s-1){
            if(val[a[L]]==1)
                --nr;
            --val[a[L]];
            ++L;
        }
        secs+=i-L+1;
    }
    memset(val,0,sizeof(int)*2000000);
    L=1; nr=0;
    for(int i=1;i<=n;i++){
        if(val[a[i]]==0)
            ++nr;
        ++val[a[i]];
        while(nr>d){
            if(val[a[L]]==1)
                --nr;
            --val[a[L]];
            ++L;
        }
        secd+=i-L+1;
    }
    fprintf(out,"%d",secd-secs);
}