Cod sursa(job #649724)

Utilizator andreioneaAndrei Onea andreionea Data 16 decembrie 2011 17:14:17
Problema Elementul majoritar Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <vector>
#include <iostream>
#define BUCKETS	66613
#define INFILE "elmaj.in"
#define OUTFILE "elmaj.out"
using namespace std;
int hash(int x)
{	
	return x % BUCKETS;
}
void addElement(vector<pair<int,int> > v[], int x, pair<int,int> &max)
{
	vector<pair<int,int> > list = v[hash(x)];
	for(vector<pair<int,int> >::iterator it = list.begin(); it != list.end(); ++it) {
		if (it->first == x) {
			++it->second;
			if (it->second > max.second)
				max = *it;
			break;
		}
		cout << it->first << endl;
	}
	pair<int,int> p(x,1);
	if (max.second < 1)
		max = p;
	list.push_back(p);
	v[hash(x)] = list;
}

int main()
{
	pair<int,int> max(0,0);
	int n;
	vector<pair<int,int> > tabela[BUCKETS];
	ifstream fin(INFILE);
	ofstream fout(OUTFILE);
	fin >> n;
	
	for(int i = 0; i<n; ++i){
		int k;
		fin >> k;
		addElement(tabela, k, max);
	}
	
	if (max.second > n/2)
		fout << max.first << " " << max.second << endl;
	else
		fout << "-1\n";
	fin.close();
	fout.close();

	return 0;
}