Cod sursa(job #674350)

Utilizator an_drey_curentandreycurent an_drey_curent Data 6 februarie 2012 01:08:37
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include<stdio.h>
#include<vector>
#include <fstream>
#define prim 123457
using namespace std;
vector<pair<int,int> >hhash[123460];
int N,numar,aux,aux2;
int operare(int x)
{
	int 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 0;
		}
		hhash[cheie].push_back(make_pair(x,1));
		if(hhash[cheie][0].second>=(aux/2+1))
			return hhash[cheie][i].second;
		else
			return 0;
		return -1;
}
int main()
{
	int ok=0;
	ifstream in("elmaj.in");
	ofstream out("elmaj.out");
	in>>N;aux=N;
	while(N--)
	{
		in>>numar;
		aux2=operare(numar);
		if(aux2>=(aux/2+1))
		{
			out<<numar<<' '<<aux2;ok=1;
			break;
		}
	}
	if(ok==0)
	out<<"-1";
	return 0;
}