Cod sursa(job #2340269)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 10 februarie 2019 10:26:23
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <unordered_map>

#define fastcall __attribute__((optimize("-O3")))
#define inline __inline__ __attribute__((always_inline))

using namespace std;

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

int v[1000005], pos = 655536;
char buff[655536];

inline fastcall char getChar()
{
    if(pos == 655536)
    {
        fread(buff, 1, 655536, stdin);
        pos = 0;
    }
    return buff[pos++];
}

inline fastcall int readInt()
{
    int nr = 0;
    char ch = getChar();
    while(1)
    {
        if(ch == ' ' || ch == '\n')
        {
            return nr;
        }
        nr = nr * 10 + (ch - '0');
        ch = getChar();
    }
    return nr;
}

unordered_map <int, int> fr;

int main()
{
    freopen("elmaj.in", "r", stdin);
    freopen("elmaj.out", "w", stdout);

    int n;
    n = readInt();

    for(int i = 1; i <= n; ++i)
    {
        int x;
        x = readInt();

        if(fr.find(x) == fr.end())
            fr[x] = 1;
        else fr[x]++;
    }

    for(auto it : fr)
    {
        if(it.second > n / 2)
        {
            printf("%d %d\n", it.first, it.second);
            return 0;
        }
    }
    printf("%d\n", -1);
    return 0;
}