Cod sursa(job #1969472)

Utilizator waren4Marius Radu waren4 Data 18 aprilie 2017 14:44:26
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

ifstream f("elmaj.in"); ofstream g("elmaj.out");

int n, x, k, a[1000004];

int main() {
    int i;
    f>>n;
    for(i = 1; i <= n; ++i) {
        f>>a[i];
    }
    k = 1;
    x = a[1];
    for(i = 2; i <= n; ++i) {
        if (a[i] ==x) {
            ++k;
        }
        else {
            --k;
        }
        if (k == 0) {
            k = 1;
            x = a[i];
        }
    }
    k = 0;
    for(i = 1; i <= n; ++i) {
        if(a[i] == x) {
            ++k;
        }
    }
    if(k > n /2) {
        g<<x<<' '<<k;
    }
    else {
        g<<-1;
    }
    return 0;
}