Cod sursa(job #1167680)

Utilizator deneoAdrian Craciun deneo Data 5 aprilie 2014 18:40:18
Problema Elementul majoritar Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

const int MAXBUFF = 10000;
const int MAXN = 500002;
char buff[MAXBUFF + 1];

#define next ++poz; if (poz >= MAXBUFF) { poz = 0; fread (buff, 1, MAXBUFF, stdin);}

int get_int() {
	static int poz = 0;
	while (buff[poz] < '0' || buff[poz] > '9') {next}
	int nr = 0;
	while (buff[poz] >= '0' && buff[poz] <= '9') {
		nr = nr * 10 + buff[poz] - '0';
		next
	}
	return nr;
}

int n, v[MAXN];
	
int main() {
	freopen("elmaj.in", "r", stdin);
	freopen("elmaj.out", "w", stdout);
	int n = get_int();
	
	int maj = 0, how_many = 0;
	
	for (int i = 1; i <= n; ++i) {
		v[i] = get_int();
		if (v[i] == maj) ++how_many;
		else --how_many;
		if (how_many == -1) {
			maj = v[i];
			how_many = 1;
		}
	}
	
	how_many = 0;
	
	for (int i = 1; i <= n; ++i) 
		if (v[i] == maj) ++how_many;
	
	if (how_many > n / 2) printf("%d %d\n", maj, how_many);
	else printf ("-1");
	
	return 0;
}