Cod sursa(job #2461285)

Utilizator Andi13Andrei Miklos Andi13 Data 25 septembrie 2019 12:15:36
Problema Elementul majoritar Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
# include <fstream>
# include <map>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int main()
{
	int n, x, ok = 0;
	int max = 0, maxv = 0, find = 0;
	map<int, int>dictionar;
	map<int, int>::iterator aux;
	fin >> n;
	for (int i = 0; i < n; i++)
	{
		fin >> x;
		aux = dictionar.find(x);
		if (aux!=dictionar.end())
		{
			aux->second++;
			if (aux->second > n/2)
			{
				find = 1;
				max = x;
				maxv = aux->second;
			}
		}
		else
			dictionar.insert(pair<int, int >(x, 1));
	}
	if (find)
		fout << max << " " << maxv;
	else
		fout << "-1";
	return 0;
}