Cod sursa(job #2554884)

Utilizator MarcGrecMarc Grec MarcGrec Data 23 februarie 2020 14:52:10
Problema Elementul majoritar Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.43 kb
#include <fstream>
#include <map>
using namespace std;

ifstream fin("elmaj.in");
ofstream fout("elmaj.out");

map<int, int> M;

int main()
{
	int n;
	fin >> n;
	bool ok = false;
	for (int i = 0, x; i < n; ++i)
	{
		fin >> x;
		++M[x];
		
		if (M[x] >= ((n / 2) + 1))
		{
			ok = true;
			fout << x << ' ' << M[x];
			break;
		}
	}
	if (!ok) { fout << "-1"; }
	
	fin.close();
	fout.close();
	return 0;
}