Cod sursa(job #1423484)

Utilizator greenadexIulia Harasim greenadex Data 21 aprilie 2015 21:22:53
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>
#include <vector>

using namespace std;

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

int main() {

    int n, nr, cand = 0, cnt = 0;

    fin >> n;
    for (; n; n--) {
        fin >> nr;
        v.push_back(nr);
        if (!cand) {
            cand = nr;
            cnt = 1;
        } else if (nr == cand)
            cnt++;
        else cnt--;
        if (!cnt)
            cand = 0;
    }
    cnt = 0;
    for (auto it : v)
        if (it == cand)
            cnt++;
    if (cnt >= n / 2 + 1)
        fout << cand << ' ' << cnt;
    else fout << -1;
    return 0;
}