Cod sursa(job #639306)

Utilizator ContraPunctContrapunct ContraPunct Data 23 noiembrie 2011 00:29:05
Problema Elementul majoritar Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include<fstream>
#include<bitset>
#include<map>
using namespace std;

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

map<long,int> hh ;
int n;
int nr;
int maxx ;
int m;
void Solve()
{
	fin>>n;
	long x;
	m = n/2+1;
	for(int i=1;i<=n;++i)
	{
		fin>>x;
		if( maxx!=0 )
			hh[maxx]++;
		else
		if( hh.find(x) == hh.end() )
		{
			hh.insert(make_pair(x,1));
		}
		else
		{
			++hh[x];
			if(hh[x] >=m)
			{
				maxx = x;
			}
		}
	}
	if(maxx != 0 )
		fout<<maxx<<" "<<hh[maxx]<<"\n";
	else
		fout<<-1;
}


int main()
{
	Solve();
	fin.close();
	return 0;
}