Cod sursa(job #2418098)

Utilizator ALEx6430Alecs Andru ALEx6430 Data 3 mai 2019 16:27:25
Problema Elementul majoritar Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <map>
using namespace std;

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

int main()
{
    ios_base::sync_with_stdio(0);
    in.tie(0);
    out.tie(0);

    int n;
    in >> n;

    map<int,int> m;
    int el = 0;
    int maj = n/2 + 1;
    bool gasit = false;

    for(int i = 0; i < n; i++)
    {
        int tmp;
        in >> tmp;

        if(!el)
        {
            int fr = ++m[tmp];

            if(!gasit && fr >= maj)
            {
                el = tmp;
                gasit = true;
            }
        }
        else if(el == tmp) m[el]++;
    }

    if(el) out << el << ' ' << m[el];
    else out << -1;

    return 0;
}