Cod sursa(job #2094157)

Utilizator arcoC. Nicolae arco Data 25 decembrie 2017 11:25:58
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <algorithm>
#include <map>
#include <fstream>

using namespace std;

int main(void)
{
    ifstream inp("elmaj.in");
    ofstream out("elmaj.out");

    if(inp.is_open() && out.is_open())
    {
        map<int, int> hashtable;

        int n;
        inp >> n;
        for(int i = 0; i < n; i++)
        {
            int t;
            inp >> t;

            hashtable[t]++;
        }

        map<int, int>::iterator it;
        for(it = hashtable.begin(); it != hashtable.end(); it++)
        {
            if(it->second >= (n/2 + 1))
            {
                out << it->first << " " << it->second;
                break;
            }
        }

        inp.close();
        out.close();
    }
    else
    {
        cout << "error\n";
    }

  return 0;
}