Cod sursa(job #709945)

Utilizator NicuCJNicu B. NicuCJ Data 8 martie 2012 18:41:36
Problema Elementul majoritar Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.42 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, contor2=0;
	f>>n;
	for(i=1; i<=n; i++)
	{
		f>>v[i];
	}
	sort(v+1, v+n+1);
	for(i=n; i>n/2-1; i--)
	{
		if(v[i]==v[i-1])
			contor++;
		if(contor==n/2+1)
		{
			ok=true;
			g<<v[i]<<" "<<n/2+1;
		}
	}
	if(!ok)
		g<<-1;
}