Cod sursa(job #3237990)

Utilizator cezicezi Cezi cezi Data 14 iulie 2024 20:52:59
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.58 kb
#include <fstream>

using namespace std;

ifstream fin ("elmaj.in");
ofstream fout ("elmaj.out");

const int f = 1000001;
int cnt,elmaj,x,n;

int main() {

	fin >> n;
	for ( int i = 1; i <= n; ++i) {
		fin >> x;
		if ( cnt == 0) {
			elmaj = x;
			cnt = 1;
			}
		else
			{ if ( x == elmaj)
					++cnt;
			else
					--cnt;
			}
 		}
 	fin.close();
 	ifstream fin ("elmaj.in");
 	fin >> n;
	cnt = 0;
 	for ( int i = 1; i <= n; ++i) {
		fin >> x;
		if ( x == elmaj)
			++cnt;
		}
	if ( cnt > n / 2)
		fout << elmaj << " " << cnt;
	else
		fout << -1;
}