Cod sursa(job #1612765)

Utilizator andreea_zahariaAndreea Zaharia andreea_zaharia Data 25 februarie 2016 00:24:05
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <cstdio>

const int NMAX = 1000010;
int v[NMAX];
int N;

int main () {
    freopen ("elmaj.in", "r", stdin);
    freopen ("elmaj.out", "w", stdout);

    scanf ("%d", &N);
    for (int i = 1; i <= N; i++) {
        scanf ("%d", &v[i]);
    }

    int candidat = v[1];
    int scor = 1;
    for (int i = 2; i <= N; i++) {
        if (v[i] == candidat) scor++;
        else scor--;

        if (scor <= 0) {
            candidat = v[i];
            scor = 1;
        }
    }

    scor = 0;
    for (int i = 1; i <= N; i++) {
        if (v[i] == candidat)
            scor++;
    }

    if (scor > N / 2) {
        printf ("%d %d\n", candidat, scor);
    }
    else {
        printf ("-1\n");
    }

    return 0;
}