Cod sursa(job #1171674)

Utilizator roxana.istratePoenaru Roxana roxana.istrate Data 16 aprilie 2014 01:54:29
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <unordered_map>

using namespace std;

int N;

unordered_map<int, int> mappings;
unordered_map<int,int>::iterator it;

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

int main(){
	fin >> N;
	int x, count = -1, who = -1;
	
	for(int i = 0; i < N; i++){
		fin >> x;
		it = mappings.find(x);
		if(it == mappings.end())
			mappings[x] = 1;
		else
			mappings[x] += 1;
	}
	
	for (unordered_map<int,int>::iterator it=mappings.begin(); it!=mappings.end(); ++it){
		if(it->second > count){
			count = it->second;
			who = it->first;
		}
	}
	if(count >= N/2 + 1)
		fout << who << " " << count << "\n";
	else
		fout << "-1\n";
	return 0;
}