Cod sursa(job #1656335)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 19 martie 2016 10:24:40
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
using namespace std;
map<int,int>m;
vector<int>a;
int n;
int main()
{
    ifstream fin("elmaj.in");
    fin>>n;
    for(int i=0;i<n;i++)
    {
        int element;
        fin>>element;
        m[element]++;
    }
    map<int,int>::iterator it=m.begin();
    map<int,int>::iterator stop=m.end();
    ofstream fout("elmaj.out");
    for(;it!=stop;it++)
    {
        if(it->second>=(n/2+1))
        {
            fout<<it->first<<' '<<it->second<<'\n';
            return 0;
        }
    }
    fout<<-1<<'\n';
    return 0;
}