Cod sursa(job #2084844)

Utilizator sandu.m.mdMorari Sandu sandu.m.md Data 9 decembrie 2017 12:17:14
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <map>
#include <fstream>

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


int main(){
map<long int, long int> vec;
long int n, element, max = 0, key;
    fin >> n;
   
    for(long int i = 0; i < n; i++){
        fin >> element;
        vec[element]++;
        if(max < vec[element]){
            max = vec[element];
            key = element;
        }
    }
/*
    for(map<long int, long int>::iterator it = vec.begin(); it != vec.end(); it++){
        if(max < it->second){
            max = it->second;
            key = it->first;
        }
    }
*/
    if(max >= n / 2 + 1)
        fout << key << " " << vec[key];
    else 
        fout << "-1";
    return 0;
}