Cod sursa(job #1072572)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 4 ianuarie 2014 16:52:53
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iterator>
#include <assert.h>
using namespace std;


const string file = "elmaj";

const string infile = file + ".in";
const string outfile = file + ".out";

const int INF = 0x3f3f3f3f;

const int MODN = 66697;


vector<vector<pair<int, int> > > HashTable;


inline int hashit(int key)
{
	return key % MODN;
}


int increment(int key)
{
	int h = hashit(key);
	for(vector<pair<int, int> >::iterator itr = HashTable[h].begin();
		itr != HashTable[h].end();
		itr++)
	{
		if(itr->first == key)
		{
	
			itr->second ++;
			return itr->second;
		}
	}

	
	HashTable[h].push_back(make_pair(key, 1));
	return 1;
	
}


int main()
{
#ifdef ONLINE_JUDGE
	ostream &fout = cout;
	istream &fin = cin;
#else
	fstream fout(outfile.c_str(), ios::out);
	fstream fin(infile.c_str(), ios::in);
#endif	

	int N;
	fin >> N;
	HashTable.resize(MODN);


	int element = 0;
	int count = 0;

	for(int i = 0; i < N; i++)
	{
		int x;
		fin >> x;
		int c = increment(x);
		if(c > count)
		{
			count = c;
			element = x;
		}
	}


	if(count >= N / 2 + 1)
	{
		fout << element << " " << count << "\n";
	}
	else
	{
		fout << "-1\n";
	}

#ifdef ONLINE_JUDGE
#else

	fin.close();
	fout.close();
#endif
}