Cod sursa(job #1355094)

Utilizator dorumusuroiFMI - Doru Musuroi dorumusuroi Data 22 februarie 2015 13:05:04
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <cstdio>
#include <map>
using namespace std;
int main ()
{
    FILE *f=fopen("majoritar.in","r");
    FILE *g=fopen("majoritar.out","w");
    map <int, int> nr;
    map<int,int>::iterator it;
    int n,i,x,ok=0;
    fscanf(f,"%d",&n);
    for(i=0;i<n;i++)
    {
        fscanf(f,"%d",&x);
        nr[x]++;
    }
    for(it = nr.begin();it!=nr.end();++it)
    {
        if(it->second>=(n/2)+1)
            {
                fprintf(g,"%d %d",it->first, it->second);
                ok=1;
                break;
            }
    }
    if(ok==0)
        fprintf(g,"-1");
    return 0;
}