Cod sursa(job #2418099)

Utilizator ALEx6430Alecs Andru ALEx6430 Data 3 mai 2019 16:32:05
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <map>
#include <unordered_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;

    unordered_map<int,int> m1;

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

        m1[tmp]++;
    }

    map<int,int> m2;

    for(auto it = m1.begin(); it != m1.end(); it++) m2.insert({it->second,it->first});

    auto it = m2.end(); it--;
    int nr = it->second;
    int ap = it->first;

    if(ap >= n/2 + 1) out << nr << ' ' << ap;
    else out << -1;

    return 0;
}