Cod sursa(job #2434262)

Utilizator ShayTeodor Matei Shay Data 1 iulie 2019 12:55:18
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <stdio.h>

inline void print(int n) {
	char snum[65];
	int i = 0;
	do {
		snum[i++] = n % 10 + '0';
		n /= 10;
	} while (n);

	--i;

	while (i >= 0) {
		putchar(snum[i--]);
	}
}

inline int next_int() {
	int n = 0;
	char c = getchar_unlocked();
	
	while (!('0' <= c && c <= '9')) {
		c = getchar_unlocked();
	}
	
	while ('0' <= c && c <= '9') {
		n = n * 10 + c - '0';
		c = getchar_unlocked();
	}

	return n;
}

int main() {
	int n, k = 0, count = 0;
	freopen("elmaj.in", "r", stdin);
	freopen("elmaj.out", "w", stdout);

	n = next_int();
	int v[n];
	
	for (int i = 0 ; i < n ; ++i) {
		v[i] = next_int();

		if (count == 0) {
			k = v[i];
		}
	
		if (v[i] == k) {
			++count;
		} else {
			--count;
		}
	}
	
	if (count <= 0) {
		printf("-1\n");
	} else {
		count = 0;
		for (int i = 0 ; i < n ; ++i) {
			if (v[i] == k) {
				++count;
			}
		}
	}
	
	print(k), putchar(' '), print(count), putchar('\n');

	return 0;
}