Cod sursa(job #2177844)

Utilizator ZappaManIosif Adrian-Mihai ZappaMan Data 18 martie 2018 21:11:29
Problema Combinari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <iostream>
#include <fstream>

using namespace std;

#define N_ 1000004

int a[N_];

int main() {
	ifstream iff("elmaj.in");
	ofstream off("elmaj.out");
	int N;
	iff >> N;
	int i = 0;
	
	int k = 0;
	int cand = -1;
	int aux = N;
	while (aux--) {
		iff >> a[i++];
		if (k == 0) {
			cand = a[i - 1];
			k++;
		}
		else if (cand == a[i - 1]) {
			k++;
		}
		else {
			k--;
		}
	}

	if (cand < 0) {
		off << cand;
	}
	else {
		int n = 0;
		for (int i = 0; i < N; ++i) {
			if (a[i] == cand) {
				n++;
			}
		}
		if (n > N / 2) {
			off << cand << " " << n;
		}
		else {
			off << -1;
		}
	}


	return 0;
}