Cod sursa(job #1423470)

Utilizator greenadexIulia Harasim greenadex Data 21 aprilie 2015 21:03:21
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>

using namespace std;

int main() {
    ifstream fin("elmaj.in");
    ofstream fout("elmaj.out");

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

    fin >> n;
    for (; n; n--) {
        fin >> nr;
        if (!cand) {
            cand = nr;
            cnt = 1;
        } else if (nr == cand)
            cnt++;
        else cnt--;
    }
    fin.close();
    ifstream fin2("elmaj.in");
    fin2 >> n;
    cnt = 0;
    for (int i = 1; i <= n; i++) {
        fin2 >> nr;
        if (nr == cand)
            cnt++;
    }
    if (cnt >= n / 2 + 1)
        fout << cand << ' ' << cnt;
    else fout << -1;
    return 0;
}