Cod sursa(job #2787834)

Utilizator AlexTacuTacu Alexandru AlexTacu Data 24 octombrie 2021 10:20:43
Problema Elementul majoritar Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

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

map < int , int > m;

int n;

void prg()
{
    in>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        in>>x;
        ++m[x];
    }
    map<int, int>::iterator itr;
    for (itr = m.begin(); itr != m.end(); ++itr)
    {
        {
            if((*itr).second>=n/2+1)
                {
                    out<<(*itr).first<<' '<<(*itr).second;
                    return ;
                }
        }
    }
    out<<-1;
}

int main()
{
    prg();
    in.close();
    out.close();
    return 0;
}

/*
#include <bits/stdc++.h>
using namespace std;
ifstream f("map.in"); ofstream g("map.out");
map <long long, int> m;
int main()
{   int n;
    f>>n;
    while(n--)
    {   long long x;
        f>>x; g<<++m[x]<<' ';
    }
    g.close(); return 0;
}
*/