Cod sursa(job #809374)

Utilizator vladm97Matei Vlad vladm97 Data 8 noiembrie 2012 10:46:08
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include<fstream>
#include<vector>
#define cst 294001

using namespace std;

vector<int>v[cst];//crearea unui vector de liste de lung cst

int cautare(int x)
{	
	int i,rest;
	rest=x%cst;
	for(i=0;i<v[rest].size();i++)//verific pe coloana pe care impartit la cst e egal. Fct size imi da nr de elem din lista v[i]
		if(x==v[rest][i])
			return i;
	return -1;
}

void inserare(int x)
{
	int rest;
	rest=x%cst;
	if(cautare(x)==-1)
		v[rest].push_back(x);// se insereaza element pe pozitia v[rest][k], un nr x ce are prop ca impartit la cst da restul rest. Str astfel obtinuta se aseamana cu o matrice de forma  0  1  2  3 4 (in acest caz am luat cst=5)
}// 																								     6  12 8 9	
void sterge(int x)		//																				     1  22									
{	int rest, i;       //																					        7				
	int p=cautare(x);
	rest=x%cst;
	if(p!=-1)
		v[rest].erase(v[rest].begin()+p,v[rest].begin()+p+1);
}
int main()
{
	int n,i,opt,x;
	ifstream f("hashuri.in");
	ofstream g("hashuri.out");
	f>>n;
	for(i=1;i<=n;i++)
	{
		f>>opt>>x;
		if(opt==1)inserare(x);
		else if(opt==2)sterge(x);
		else if(opt==3)
			{
				if(cautare(x)==-1)
					g<<0<<'\n';
				else g<<"1\n";
		}
	}
}