Cod sursa(job #1455491)

Utilizator Player1Player 1 Player1 Data 28 iunie 2015 03:34:40
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <stdio.h>
#include <map>

using namespace std;

int main(){
	freopen("elmaj.in","r",stdin);
	freopen("elmaj.out","w",stdout);

	int N, x, i;
	map<int,int> occurances;
	bool flag = false;
	int candidate;

	scanf("%d ", &N);

	for (i=0; i<N; i++){
		scanf("%d ", &x);
		if(occurances.find(x) == occurances.end())
			occurances[x] = 1;
		else{
			occurances[x] ++;
			if(occurances[x] >= N/2 +1){
				candidate = x;				
				flag = true;
			}
		}
	}

	if(flag == false)
		printf("-1");
	else
		printf("%d %d", candidate, occurances[candidate]);
	return 0;
}