Cod sursa(job #1806981)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 15 noiembrie 2016 21:22:23
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
map<int,int>m;
int a[1000000];
int n;
int main()
{
    freopen("elmaj.in","r",stdin);
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        m[a[i]]++;
    }
    freopen("elmaj.out","w",stdout);
    for(int i=0;i<n;i++)
    {
        if(m[a[i]]>=n/2+1)
        {
            printf("%d %d",a[i],m[a[i]]);
            return 0;
        }
    }
    printf("-1");
    return 0;
}