Cod sursa(job #1455495)

Utilizator Player1Player 1 Player1 Data 28 iunie 2015 03:39:34
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 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(flag == false){
			if(occurances.find(x) == occurances.end())
				occurances[x] = 1;
			else{
				occurances[x] ++;
				if(occurances[x] >= N/2 +1){
					candidate = x;				
					flag = true;
				}
			}
		} else {
			if (x == candidate)
				occurances[x] ++;
		}
	}

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