Cod sursa(job #645745)

Utilizator DaicuDaicu Alexandru Daicu Data 10 decembrie 2011 13:59:03
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include<fstream>
#include<vector>
#define hk 666013
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");

int elmaj,nap,n;
vector < pair <int,int> > hash_map[666013];

int main(){
	f>>n;
	int x,verif;
	for(int i=1;i<=n;i++){
		f>>x;
		verif=0;
		if(!hash_map[x%hk].empty()){
		for(unsigned int j=0;j<=hash_map[x%hk].size();j++)
			if(hash_map[x%hk][j].first==x){
				hash_map[x%hk][j].second++;
				verif=1;
				if(nap<hash_map[x%hk][j].second){
					nap=hash_map[x%hk][j].second;
					elmaj=x;
				}
					
			}
		}
		if(verif==0)
			hash_map[x%hk].push_back(make_pair(x,1));
	}
	if(nap>=n/2+1){
		g<<elmaj<<" "<<nap;
	}
	else
		g<<"-1";
	return 0;
}