Cod sursa(job #681951)

Utilizator geobarosanu1Tutuianu George geobarosanu1 Data 18 februarie 2012 11:50:43
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.95 kb
#include <stdio.h>
#include <vector>
#include <queue>
#define max 123457;
using namespace std;

vector <unsigned int> lista;
vector <pair <unsigned int,unsigned int> > hhash[123458];

bool search(unsigned int x, int cheie){

	for (unsigned int j=0;j<hhash[cheie].size();j++)
		if (hhash[cheie][j].first==x && hhash[cheie][j].second!=0)
				return 1;
	return 0;
};
int verifica(queue <unsigned int> coada){
	if (coada.empty())
		return 0;
	int sol=0,x=coada.front(),y;
	coada.pop();
	for (;!coada.empty();){
		y=coada.front();
		coada.pop();
		if (x==y)
			sol++;
		else
			return sol;
		x=y;
	}
	return sol;
}
int calculare(unsigned int secventa_cautata){

	queue <unsigned int> coada;
	unsigned int solutie=0,lungime=0,cheie;

	for (unsigned int i=0;i<lista.size();i++){
		
		cheie=lista[i]%max;
		if (!search(lista[i],cheie)){

			if (lungime<secventa_cautata){
				///////////// adauga un element //////////////
				hhash[cheie].push_back(make_pair(lista[i], 1));
				lungime++;
				coada.push(lista[i]);
				//////////////////////////////////////////////
				if (lungime == secventa_cautata)
					solutie++;
			}	//	elementul nu se gaseste in coada si lungimea subsirului este mai mica

			else {
				
				solutie+=verifica(coada);
	
				while (lungime >= secventa_cautata){
					int x=coada.front();
					int cheie2=x%max;
					coada.pop();
					//////// Elimina un element ////////
					for (int j=0;j<hhash[cheie2].size();j++)
						if (hhash[cheie2][j].first==x){
							hhash[cheie2][j].second--;
							if (!hhash[cheie2][j].second){
								lungime--;
								hhash[cheie2][j].first=0;
							}
							continue;
						}
					////////////////////////////////////
					if (lungime == secventa_cautata)
						solutie++;
				}	// end_while -- lungimea a devenit mai mica
				coada.push(lista[i]);
				lungime++;
				hhash[cheie].push_back(make_pair(lista[i],1));
				solutie++;
			}	// end_if_else elementul nu se gaseste in coada si lungimea este egala
		}	// end_if_then -- elementul nu se gaseste in coada

		else {
			coada.push(lista[i]);
			for (int j=0;j<hhash[cheie].size();j++)
				if (hhash[cheie][j].first==lista[i]){
					hhash[cheie][j].second++;
					continue;
				}
			if (lungime==secventa_cautata)
				solutie++;

		}

	}
	
	while (coada.size()!=0){
		int x=coada.front();
		cheie=x%max;
		coada.pop();
		for (int i=0;i<hhash[cheie].size();i++)
			if (hhash[cheie][i].first==x){
				hhash[cheie][i].second--;
				if (hhash[cheie][i].second==0){
					hhash[cheie][i].first=0;
					lungime--;
				}
				continue;
			}
			if (lungime==secventa_cautata)
				solutie++;
	}
		
	return solutie;
}

int main()
{
	freopen("secv5.in","r",stdin);
	freopen("secv5.out","w",stdout);
	unsigned int n,l,u;
	unsigned int x;

	scanf("%d%d%d", &n, &l, &u);
	while (n--){
		scanf("%d", &x);
		lista.push_back(x);
	}
	
	l=calculare(l);
	u=calculare(u);
	printf("%d", l+u);

	return 0;
}