Cod sursa(job #2553182)

Utilizator BarsanEmilianIoanBarsan Emilian-Ioan BarsanEmilianIoan Data 21 februarie 2020 18:32:11
Problema Elementul majoritar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
# include <bits/stdc++.h>

using namespace std;

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

int n, x, elmaj, f, s;

int main ()

{
    fin >> n;
    fin >> x;
    elmaj = x;
    f = 1;
    for ( int i = 1; i < n; ++i){
        fin >> x;
        if ( f < 0)
            elmaj = x;
        if (elmaj == x)
            ++f;
        else
            --f;
    }
    fin.close();
    fout.close();
    ifstream fin ("elmaj.in");
    ofstream fout ("elmaj.out");
    for ( int i = 1; i <= n; ++i){
        fin >> x;
        if(x == elmaj)
            ++f;
    }
    if(f > n/2)
        fout << elmaj << " " << f;
    else
        fout << "-1";
    return 0;
}