Cod sursa(job #692507)

Utilizator AndreyPAndrei Poenaru AndreyP Data 26 februarie 2012 16:43:08
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
using namespace std;

#define N 1000010

int n;
int v[N];

inline void solve() {
    int who = 0, cnt = 0;
    ifstream fin("elmaj.in");

    fin >> n;
    for (int i = 0; i < n; ++i) {
        fin >> v[i];

        if (cnt == 0) {
            who = v[i];
            ++cnt;
            continue;
        }

        if (v[i] == who) {
            ++cnt;
        } else {
            --cnt;
        }
    }

    fin.close();
    ofstream fout("elmaj.out");

    if (cnt == 0) {
        fout << "-1\n";
        fout.close();

        return;
    }

    cnt = 0;
    for (int i = 0; i < n; ++i) {
        if (v[i] == who) {
            ++cnt;
        }
    }

    if (cnt > n / 2) {
        fout << who << " " << cnt << "\n";
    } else {
        fout << "-1\n";
    }

    fout.close();
}

int main() {
    solve();

    return 0;
}