Cod sursa(job #1464356)

Utilizator tony.hegyesAntonius Cezar Hegyes tony.hegyes Data 23 iulie 2015 08:46:20
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <fstream>
using namespace std;

///// DESCRIPTION
// THIS PROGRAM FINDS THE MAJORITY
// ELEMENT FROM A VECTOR OF N ELEMENTS 
// MAJ EL = APPEARS AT LEAST (N/2) + 1 TIMES
/////

int main(int argc, char **argv)
{
	// INPUT
	int n;
	ifstream indata("elmaj.in");
	indata >> n;	
	

	int freq[2000000001], max = 0;
	for (int i = 1; i <= 2000000001; i++) {
		freq[i] = 0;
	}
	int aux;
	for (int i = 0; i < n; i++) {
		indata >> aux;
		freq[aux]++;
		if (freq[aux] > freq[max]) {
			max = aux;
		}
	}
	indata.close();
	
	// FIND A MAJORITY ELEMENT AND OUTPUT IT
	ofstream outdata("elmaj.out");
	if (freq[max] >= (n /2) + 1) {
		outdata << max << " " << freq[max];
	} else {
		outdata << -1 << " " << 0;
	}
	outdata.close();

	return 0;
}