Cod sursa(job #2438468)

Utilizator BarsanEmilianIoanBarsan Emilian-Ioan BarsanEmilianIoan Data 12 iulie 2019 16:10:38
Problema Elementul majoritar Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
# include <iostream>
# include <fstream>
# include <cmath>

using namespace std;

ifstream  fi("elmaj.in");
ofstream fo("elmaj.out");

long long n, x, elmaj, s;

int main ()

{
    fi >> n;
    for ( int i = 1; i <= n; ++i) {
    fi >> x;
    if ( s == 0) {
        elmaj = x;
        s = 1;
    }
    else
        if ( x == elmaj)
            ++s;
        else
            --s;
    }
    fi.close();
    ifstream  fi("elmaj.in");
     fi >> n;
     s = 0;
    for ( int i = 1; i <= n; ++i) {
    fi >> x;
    if ( x == elmaj)
            ++s;
    }
    if ( s >= n / 2 + 1)
        fo << elmaj << " " << s;
    else
        fo << -1;
    return 0;
}