Cod sursa(job #2799211)

Utilizator Madalin_IonutFocsa Ionut-Madalin Madalin_Ionut Data 12 noiembrie 2021 17:30:19
Problema Secventa 5 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, st, dr, a[1048580];
unordered_map<int, int> M;

/// <summary>
///  5 2 3
/// 13 13 7 9 9 13 9 13
/// 
/// nregale = 1
/// fr 1 2 3 4 5 6 7 8 9 10 11 12 13
///    0 0 0 0 0 0 1 0 3  0  0  0  4
/// cnt = 2 + 3 + 3 + 5 + 6 + 7
/// </summary>
/// <returns></returns>

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

void Rezolvare()
{
	int i, j, nregale = 0, cnt = 0, nrdist = 0;
	i = 1;
	for (j = 1; j <= n; j++)
	{
		if (a[j] != a[j - 1]) nregale = 0;
		while (nrdist > dr)
		{
			M[a[i]]--;
			if (M[a[i]] == 0) nrdist--;
			i++;
		}
		M[a[j]]++;
		if (M[a[j]] == 1) nrdist++;
		if (nrdist >= st) cnt += (j - i - nregale);
		nregale++;
	}
	fout << cnt << "\n";
}

int main()
{
	Citire();
	Rezolvare();
}