Cod sursa(job #1066400)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 24 decembrie 2013 17:38:54
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.63 kb
#include<fstream>
#include<unordered_map>

using namespace std;

ifstream f("elmaj.in");
ofstream g("elmaj.out");

int n, elem_maj, elem_maj_frecv;
unordered_map<int, int> h;

int main() {

	int i, val;
	bool gata;

	f >> n;

	for (i = 1; i <= n; ++i) {
		f >> val;
		h[val]++;
	}

	gata = false;
	for (auto it = h.begin(); it != h.end() and gata != true; ++it)
		if (it->second >= n / 2 + 1) {
			elem_maj = it->first;
			elem_maj_frecv = it->second;
			gata = true;
		}

	if (gata == true)
		g << elem_maj << " " << elem_maj_frecv << "\n";
	else
		g << "-1\n";

	f.close();
	g.close();

	return 0;
}