Cod sursa(job #2094159)

Utilizator arcoC. Nicolae arco Data 25 decembrie 2017 11:29:07
Problema Elementul majoritar Scor 50
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;
        bool found = false;
        for(int i = 0; i < n; i++)
        {
            int t;
            inp >> t;

            hashtable[t]++;

            if(hashtable[t] >= (n/2 + 1))
            {
                found = true;
                out << t << " " << hashtable[t];
                break;
            }
        }
        if(!found)
        {
            out << "-1";
        }
        inp.close();
        out.close();
    }
    else
    {
        cout << "error\n";
    }

  return 0;
}