Cod sursa(job #1154859)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 26 martie 2014 14:02:02
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <hash_map>

using namespace std;
using namespace __gnu_cxx;

hash_map<int, int> m;
int n, ok;

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

    int t;
    fin >> n;
    for(int i=0; i<n; i++){
        //scanf("%d ", &t);
        fin >> 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;
        }
    }*/
    for(hash_map<int, int>::iterator i=m.begin(); i!=m.end(); i++){
        if(i->second >= n/2+1){
            //printf("%d %d\n", i->first, i->second);
            fout << i->first << " " << i->second << "\n";
            ok = 1;
            break;
        }
    }
    if(!ok) printf("-1\n");

    return 0;
}