Cod sursa(job #2553200)

Utilizator BarsanEmilianIoanBarsan Emilian-Ioan BarsanEmilianIoan Data 21 februarie 2020 18:50:38
Problema Elementul majoritar Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 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");

    fin >> n;

    f = 0;

    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;


}