Cod sursa(job #2755645)

Utilizator lahayonTester lahayon Data 27 mai 2021 22:08:44
Problema Secventa 5 Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <vector>


using namespace std;

int sequences(int limit, vector<unsigned int> values){

    int result = 0;
    unordered_map<unsigned int, int> hashtable;
    unordered_map<unsigned int, int>::iterator it;

    int left = 0;

    for(int i = 0; i < values.size(); ++i){

        it = hashtable.find(values[i]);
        if(it != hashtable.end())
            ++it->second;
        else hashtable.insert(pair<unsigned int, int>(values[i], 1));

        while(hashtable.size() > limit){
            it = hashtable.find(values[left]);
            if(--it->second == 0) hashtable.erase(values[left]);
            ++left;
        }
         result += (i - left + 1);
    }
    return result;

}

int main()
{ 

    ifstream cin("secv5.in");
    ofstream cout("secv5.out");

    int N, L, U;
    cin >> N >> L >> U;
    
    vector<unsigned int> values(N);


    for(int i = 0; i < N; ++i)
        cin >> values[i];

    cout << sequences(U, values) - sequences(L - 1, values);

    cin.close();
    cout.close();

  return 0;
}