Cod sursa(job #2038989)

Utilizator BourucLiviuBouruc Petru Liviu BourucLiviu Data 14 octombrie 2017 10:30:04
Problema Secventa 5 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <unordered_map>

#define NMAX 1024*1024 + 5

using namespace std;

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

unsigned a[NMAX], n, L, U;

/// cate secvente au cel mult k valori distincte
long long NrSecvente(int k)
{
    unordered_map <unsigned, int> M;
    int i, j, x;
    long long cnt = 0;
    i = 1;
    for(j = 1; j <= n; ++j)
    {
        M[a[j]]++;
        while(M.size() > k)
        {
            x = a[i];
            i++;
            M[x]--;
            if(M[x] == 0) M.erase(x);
        }
        cnt += (j-i+1);
        /// adaug cele j-i+1 secv care se termina cu a[j]
    }
    return cnt;
}

int main()
{
    fin >> n >> L >> U;
    for(int i = 1; i <= n; ++i)
        fin >> a[i];
    fout << NrSecvente(U) - NrSecvente(L-1);
    return 0;
}