Cod sursa(job #2587498)

Utilizator k2e0e0w3qDumitrescu Gheorghe k2e0e0w3q Data 22 martie 2020 19:22:57
Problema Elementul majoritar Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;

int main () {
    ifstream fin ("elmaj.in");
    ofstream fout ("elmaj.out");
    vector <int> v;

    int n, m, x, ap=0;
    fin >> n >> m;
    v.push_back(m);
    int k=n>>1;

    for (--n; n; n--) {
        fin >> x;
        v.push_back(x);
        if (x==m)
            ap++;
        else {
            ap--;
            if (!ap)
                ap=1,
                m=x;
        }
    }

    for (auto it: v)
        if (it==m)
            ap++;
    if (ap>k)
        fout << m << ' ' << ap;
    else
        fout << -1;

    fout << endl;
    fout.close();
    return 0;
}