Cod sursa(job #2870833)

Utilizator Fantastic_Mantudor voicu Fantastic_Man Data 12 martie 2022 16:43:07
Problema Elementul majoritar Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>

using namespace std;


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

int main() {
    int n, x, elem = -1, ap = 0;
    fin >> n;
    for ( int i = 1; i <= n; i++ ) {
        fin >> x;
        if ( x != elem ) {
            ap--;
            if ( ap < 0 )
                elem = x, ap = 1;
        } else
            ap++;
    }

    fin.close ();
    ifstream fin ( "elmaj.in" );
    fin >> n;
    ap = 0;
    for ( int i = 1; i <= n; i++ )
        fin >> x, ap += ( x == elem );
    if ( ap > n / 2 )
        fout << elem << ' ' << ap << '\n';
    else
        fout << -1 << '\n';



    return 0;
}