Cod sursa(job #2038973)

Utilizator loo_k01Luca Silviu Catalin loo_k01 Data 14 octombrie 2017 10:25:13
Problema Secventa 5 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

int a[1024 * 1024 + 20], n, L, U;

///cate secvente au cel mult k valori distincte
unsigned long long NrSecvente(int K)
{
    unordered_map<int, int> M;
    unsigned long long i, j, x;
    unsigned 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 secvente care se termina cu a[j]
    return cnt;
}

void Read()
{
    ifstream fin("secv5.in");
    fin >> n >> L >> U;

    for(int i = 1; i <= n; i++)
        fin >> a[i];
    fin.close();
}

int main()
{
    Read();
    ofstream fout("secv5.out");
    fout << NrSecvente(U) - NrSecvente(L - 1);
    return 0;
}