Cod sursa(job #2038994)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 14 octombrie 2017 10:31:42
Problema Secventa 5 Scor 50
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 0.84 kb
#include <bits/stdc++.h>

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

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

void Citire()
{
    int i;
    fin >> n >> L >> U;
    for (i=1; i<=n; i++)
        fin >> a[i];
}

/// cate secventa au cel mult k valori distincte
int NrSecvente(int K)
{
    unordered_map <unsigned, int> m;
    unsigned 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()
{
    Citire();
    fout << (NrSecvente(U)-NrSecvente(L-1)) << "\n";
    return 0;
}