Cod sursa(job #862490)

Utilizator MciprianMMciprianM MciprianM Data 22 ianuarie 2013 18:49:47
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

const int MOD = 1000033;
vector<pair<int, int> > hash_table[MOD];

int main() {
	int n, c(0), x;
	ifstream f("elmaj.in");
	ofstream g("elmaj.out");
	f >> n;
	for(int i = 1; i <= n; i++) {
		f >> x;
		int h = x % MOD;
		bool found = false;
		for(int j = 0; j < hash_table[h].size(); ++j) {
			if(hash_table[h][j].first == x) {
				hash_table[h][j].second++;
				found = true;
				break;
			}
		}
		if(!found) {
			hash_table[h].push_back(make_pair(x, 1));
		}
	}
	f.close();
	for(int i = 0; i < MOD; ++i) {
		for(int j = 0; j < hash_table[i].size(); ++j) {
			if(hash_table[i][j].second > n / 2) {
				g << hash_table[i][j].first << " " << hash_table[i][j].second << endl;
				g.close();
				return 0;
			}
		}
	}
	g << "-1\n";
	g.close();
	return 0;
}