Cod sursa(job #1060788)

Utilizator cdascaluDascalu Cristian cdascalu Data 18 decembrie 2013 18:58:20
Problema Elementul majoritar Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <cstdio>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <string>
#include <set>
#include <stack>
#include <unordered_map>
using namespace std;
void print_sol(long long x,int nr)
{
    ofstream g("elmaj.out");
    if(x == -1)
        g<<x;
    else
        g<<x<<" "<<nr;
    g.close();
}
int main()
{
    unordered_map<long long,int> hash;
    ifstream f("elmaj.in");
    int N;
    long long x;
    f>>N;
    for(int i=1;i<=N;++i)
    {
        f>>x;
        if(hash.find(x) != hash.end())
            ++hash[x];
        else
            hash[x] = 1;

        if(hash[x] > N/2)
        {
            print_sol(x, hash[x]);
            return 0;
        }
    }
    print_sol(-1, 0);
    return 0;
}