Pagini recente » Borderou de evaluare (job #3003291) | Borderou de evaluare (job #950556) | Autentificare | Monitorul de evaluare | Cod sursa (job #3351663)
#include <fstream>
#include <vector>
#include <unordered_map>
using namespace std;
int main()
{
ifstream fin("secv5.in");
ofstream fout("secv5.out");
int N, L, U;
fin >> N >> L >> U;
vector <int> hashedElements(N);
unordered_map <int, int> hashedElementsMap;
hashedElementsMap.reserve(N);
int currId = 1;
for (int i = 0; i < N; i++)
{
int currElem;
fin >> currElem;
auto it = hashedElementsMap.find(currElem);
if (it == hashedElementsMap.end()) {
hashedElementsMap[currElem] = currId;
hashedElements[i] = currId;
currId++;
}
else hashedElements[i] = it->second;
}
vector<int> countL(currId, 0);
vector<int> countU(currId, 0);
int leftU = 0, leftL = 0;
int distinctU = 0, distinctL = 0;
long long subsequenceCount = 0;
for (int right = 0; right < N; right++)
{
int currVal = hashedElements[right];
if (countL[currVal] == 0) distinctL++;
countL[currVal]++;
while (distinctL > L - 1)
{
int leftVal = hashedElements[leftL];
countL[leftVal]--;
if (countL[leftVal] == 0) distinctL--;
leftL++;
}
if (countU[currVal] == 0) distinctU++;
countU[currVal]++;
while (distinctU > U)
{
int leftVal = hashedElements[leftU];
countU[leftVal]--;
if (countU[leftVal] == 0) distinctU--;
leftU++;
}
subsequenceCount += (leftL - leftU);
}
fout << subsequenceCount;
return 0;
}