Cod sursa(job #803981)

Utilizator Dragan_ValentinDragan Valentin Dragan_Valentin Data 28 octombrie 2012 17:45:26
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

int hash_n;
vector<vector<int> > v;


void add_item(int x)
{
	for (int i=0; i<v[x%hash_n].size(); i++) {
		//cout<<"crash la "<<i<<'\n';
		if (v[x%hash_n][i]==x) return;
	}	
	//cout<<"item x added ! ";
	v[x%hash_n].push_back(x);
	//cout<<"size of "<<x%hash_n<<" is now "<< v[x%hash_n].size()<<'\n';
}

void remove_item(int x)
{
	for (int i=0; i<v[x%hash_n].size(); i++)
		if (v[x%hash_n][i]==x) {
			//cout<<"item x removed !"; 
			v[x%hash_n].erase(v[x%hash_n].begin() + i); 
			return;
		}
}

bool find(int x)
{
	//cout<<" gotta look in "<<x%hash_n<<" where size is "<<v[x%hash_n].size()<<'\n';
	for (int i=0; i<v[x%hash_n].size(); i++) {
		//cout<<"looking at "<<v[x%hash_n][i]<<'\n';
		if (v[x%hash_n][i]==x) return true;
	}
	return false;
}


void create_vector(int n)
{
	hash_n=sqrt((float)n)*log(n);
	v.resize(hash_n);
	for (int i=0; i<v.size(); i++)
		v[i]=vector<int>();
}

int main()
{
	int i,n;
	ifstream f("hashuri.in");
	ofstream g("hashuri.out");
	f>>n;
	
	create_vector(n);
	//cout<<hash_n<<'\n';
	int x;
	int oper;
	for (i=0; i<n; i++) {
		f>>oper>>x;
		if (oper==1) { 
			//cout<<i<<" additem "<<'\n'; 
			add_item(x);
			}
		else if (oper==2) {
			//cout<<i<<" removeitem "<<'\n';
			remove_item(x); 
			}
		else if (oper==3 && find(x)==true) {
			//cout<<i<<" find true "; 
			g<<1<<'\n'; 
			}
		else if (oper==3 && find(x)==false) {
			//cout<<i<<" find false "<<'\n';
			g<<0<<'\n';
			}
	}
	f.close();
	g.close();
	return 0;
}