Cod sursa(job #1154406)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 26 martie 2014 10:13:05
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <iostream>
#include <cstdio>
#include <map>

using namespace std;

map<int, int> m;
int n, ok, a[1000005];

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

    int t;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("%d ", &t);
        a[i] = t;
        m[t]++;
    }
    for(int i=0; i<n; i++){
        if(m[a[i]] >= n/2+1){
            printf("%d %d\n", a[i], m[a[i]]);
            ok=1;
            break;
        }
    }
    if(!ok) printf("-1\n");

    return 0;
}