Cod sursa(job #2069936)

Utilizator AlexaCatanaCatana Alexandra-Vasilica AlexaCatana Data 18 noiembrie 2017 23:37:27
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <map>
#include <string>
#include <fstream>

using namespace std;

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

	int n, x, i;
	f >> n;
	map<int, int> m;

	for (i = 1; i <= n; ++i)
	{
		f >> x;
		if (m.find(x) == m.end())
		{
			m.insert(make_pair(x, 1));
		}
		else
		{
			++m[x];
		}
	}

	map<int, int>::iterator p;
	map<int, int>::iterator it;

	int max = -1;

	for (it = m.begin(); it != m.end(); ++it)
	{
		if (it->second > max)
		{
			max = it->second;
			p = it;
		}
	}

	if (max < n / 2 + 1)
		g << "-1";
	else
		g << p->first << " " << p->second;

	//system("pause");
	return 0;
}