Cod sursa(job #709959)

Utilizator NicuCJNicu B. NicuCJ Data 8 martie 2012 18:53:42
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.48 kb
#include <fstream>
#include <algorithm>
using namespace std;
int v[1000001];
int main()
{
	ifstream f("elmaj.in");
	ofstream g("elmaj.out");
	bool ok=false;
	int n, j, i, contor=1, max=0;
	f>>n;
	for(i=1; i<=n; i++)
	{
		f>>v[i];
	}
	sort(v+1, v+n+1);
	for(i=n; i>1; i--)
	{
		if(v[i]==v[i-1])
			contor++;
		else
			contor=1;
		if(contor==n/2+1)
		{
			ok=true;
			g<<v[i];
		}
		if(contor>max)
			max=contor;
	}
	if(ok)
		g<<" "<<max;
	if(!ok)
		g<<-1;
}