Cod sursa(job #1204894)

Utilizator pulseOvidiu Giorgi pulse Data 4 iulie 2014 13:04:12
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>

using namespace std;

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

int n, v[1000001], count, maj = -1;

int main ()
{
	fin >> n;
	for (int i = 1; i <= n; ++i)
	{
		fin >> v[i];
		if (count == 0)
		{
			maj = v[i];
			count = 1;
		}
		else if (v[i] == maj)
			++count;
		else
			--count;
	}
	count = 0;
	for (int i = 1; i <= n; ++i)
		if (v[i] == maj)
			++count;
	if (count >= (n / 2) + 1)
		fout << maj << " " << count << '\n';
	else
		fout << "-1" << '\n';
	fin.close();
	fout.close();
	return 0;
}