Cod sursa(job #1577113)

Utilizator vladvlad00Vlad Teodorescu vladvlad00 Data 23 ianuarie 2016 11:29:43
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>

using namespace std;

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

int n, cand, negrup, nr, v[1000005];

int main()
{
	int i;

	fin >> n;
	fin >> v[1];
	cand = v[1];
	negrup = 1;
	for (i = 2; i <= n; i++)
	{
		fin >> v[i];
		if (negrup == 0)
			cand = v[i];
		if (v[i] == cand)
			negrup++;
		else negrup--;
	}
	for (i = 1; i <= n; i++)
		if (v[i] == cand)
			nr++;
	if (nr > n / 2)
		fout << cand << ' ' << nr << '\n';
	else fout << -1 << '\n';
	return 0;
}