Cod sursa(job #568183)

Utilizator MciprianMMciprianM MciprianM Data 30 martie 2011 21:43:28
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.09 kb
//#include <fstream>
//#include <iostream>
//#include <ctime>
//#include <cstdlib>
//#include <cstring>
//
//using namespace std;
//
//const int p = (1<<20);
//const int slim = 1000;
//
//int hTable [ p ];
//
//int ok, nok;
//int nfound, nnfound;
//
//int maxu, maxs;
//
//int hash ( int x, int i ){
//	return ( x + ( i * ( i + 1 ) >> 1 ) ) % p;
//}
//
//void insert ( int v ){
//	int i;
//	for ( i = 0; hTable [ hash ( v, i ) ] != -1; ++ i );
//	hTable [ hash ( v, i ) ] = v;
//}
//
//int search ( int v ){
//	int i;
//	for ( i = 0; hTable [ hash ( v, i ) ] != -1 && hTable [ hash ( v, i ) ] != v; ++ i );
//
//	if ( hTable [ hash ( v, i ) ] == -1 ) {
//		nnfound ++;
//		nok += ( i + 1 );
//		maxu = max ( maxu, i + 1 );
//		return -1;
//	}
//	maxs = max ( maxs, i + 1 );
//	nfound ++;
//	ok += ( i + 1 );
//	return hash ( v, i );
//}
//
//
//
//double loadFactor [] = { 0.8, 0.85, 0.9, 0.95, 0.99 };
//
//int main (){
//	int n, i, j, k;
//	srand ( ( unsigned ) time ( NULL ) );
//	double avg1, avg2;
//	double nf1, nf2;
//	for ( i = 0; i < 5; ++ i ) {
//		avg1 = 0;	avg2 = 0;
//		nf1 = 0;	nf2 = 0;
//		for ( k = 0; k < 5; ++ k ){
//			n = ( int ) ( loadFactor [ i ] * p ) + 1;
//			memset ( hTable, -1, sizeof ( hTable ) );
//			ok = nok = nfound = nnfound = 0;
//			for ( j = 0; j < n; ++ j )
//				insert ( rand () );
//			for ( j = 0; j < slim; ++ j ){
//				int hj1 = rand (), hj;
//				hj = search ( hj1 );
//				if ( hj != -1 && hTable [ hj ] != hj1 ){
//					cout << "Error !!!!!!" << endl;
//					return 0;
//				}
//
//			}
//			avg1 += ( (double)ok/(double)nfound );
//			avg2 += ( (double)nok/(double)nnfound );
//			nf1 += nfound;
//			nf2 += nnfound;
//		}
//		avg1 /= 5;	avg2 /= 5;
//		nf1 /= 5; nf2 /= 5;
//		cout << "Load Factor: " << loadFactor [ i ] << endl;
//		cout << "Succesful average: " << avg1 << endl;
//		cout << "Unsuccesful average: " << avg2 << endl;
//		cout << "Average nr of successful searches " << nf1 << endl;
//		cout << "Average nr of unsuccessful searches " << nf2 << endl;
//		cout << "Maximum effort of succesfull search " << maxs << endl;
//		cout << "Maximum effort of unsuccesfull search " << maxu << endl;
//	}
//	return 0;
//}
#include<fstream>
#include<vector>
#define prim 700001
using namespace std;
vector<int>a[(1<<20)];
vector<int>::iterator cauta(int x){
  vector<int>::iterator it;
  int l=x%prim;
  for(it=a[l].begin();it!=a[l].end();++it)
    if(*it==x)
      return it;
  return a[l].end();
}
int main(){
  int n, i,cod;
  unsigned x, A = ((1u<<31)|3462875);
  ifstream f("hashuri.in");
  ofstream g("hashuri.out");
  f>>n;
  for(i=0;i<n;i++)
  {
    f>>cod>>x;
    if(cod==1){
      if(cauta(x)==a[(A*x>>12)].end())
        a[x%prim].push_back(x);
    }
    else if(cod==3){
      if(cauta(x)!=a[(A*x>>12)].end())
           g<<"1\n";
      else g<<"0\n";
    }
    else if(cod==2){
          vector<int>::iterator it;
          it=cauta(x);
          if(it!=a[(A*x>>12)].end())
            a[(A*x>>12)].erase(it);
        }
    }
  f.close();
  g.close();
  return 0;
}