Cod sursa(job #635179)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 18 noiembrie 2011 17:56:18
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include<fstream>
#include<hash_map>
using namespace std;

const char inFile[] = "elmaj.in";
const char outFile[] = "elmaj.out";

hash_map<int, int> hashTable;
int N;

int element = -1;


void Citire();
void Afisare();
void Find(int value);


void Find(int value)
{
	hash_map<int, int>::iterator itr = hashTable.find(value);
	if(itr == hashTable.end())
	{
		hashTable.insert(make_pair(value, 1));
	}
	else
	{
		itr->second++;
		if(element == -1 && itr->second > N/2)
		{
			element = itr->first;
		}
	}
}


void Citire()
{
	fstream fin(inFile, ios::in);
	
	fin >> N;

	int current;
	for(int i = 0 ; i < N; i++)
	{
		fin >> current;
		Find(current);
	}

	fin.close();
}

void Afisare()
{
	fstream fout(outFile, ios::out);
	
	if( element == -1)
	{
		fout << "-1\n";
	}
	else
	{
		fout << element << " " << hashTable.find(element)->second << "\n";
	}
	fout.close();
}


int main(int argc, char* argv[])
{
	
	Citire();
	Afisare();
}