Cod sursa(job #674345)

Utilizator an_drey_curentandreycurent an_drey_curent Data 6 februarie 2012 00:46:45
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include<stdio.h>
#include<vector>
#define prim 123457
using namespace std;
vector<pair<long unsigned,long unsigned> >hhash[123460];
long unsigned N,numar,aux,aux2;
long unsigned operare(long unsigned x)
{
	long unsigned cheie=x%prim,i;
	for(i=0;i<hhash[cheie].size();i++)
		if(hhash[cheie][i].first==x)
		{
			hhash[cheie][i].second++;
			if(hhash[cheie][i].second>=(aux/2+1))
				return hhash[cheie][i].second;
			else
				return 1;
		}
		hhash[cheie].push_back(make_pair(x,1));
		return 0;
}
int main()
{
	freopen("elmaj.in","r",stdin);
	freopen("elmaj.out","w",stdout);
	scanf("%lu",&N);aux=N;
	while(N--)
	{
		scanf("%lu",&numar);
		aux2=operare(numar);
		if(aux2>=(aux/2+1))
		{
			printf("%lu %lu\n",numar,aux2);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}