Cod sursa(job #1171671)

Utilizator roxana.istratePoenaru Roxana roxana.istrate Data 16 aprilie 2014 01:27:19
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include <fstream>
#include <map>

using namespace std;

int N;

map<int, int> mappings;
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;
		}
		if(mappings[x] > count){
			count = it->second;
			who = x;
		}
	}
	if(count >= N/2 + 1)
		fout << who << " " << count << "\n";
	else
		fout << "-1\n";
	return 0;
}