Cod sursa(job #760577)

Utilizator bratualexBratu Alexandru bratualex Data 22 iunie 2012 00:58:08
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>

using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int main()
{
    int n,check,j,i,k=0,nrmax=0,imax=0,v[100000],x,f[100000];
    fin>>n;
    for(i=0;i<n;i++)
    {
        check=0;
        fin>>x;
        for(j=0;j<k;j++)
        {
            if(v[j]==x)
            {
                f[j]++;
                check=1;
                if (f[j]>=nrmax)
                {
                    nrmax=f[j];
                    imax=j;
                }
            }

        }
        if ( !check)
        {

            f[k]=1;
            v[k++]=x;
        }
    }
    //x=0;
   /* for(j=0;j<k;j++)
    {
        if(f[j]>x)
        {
            x=f[j];
            imax=j;
        }
    }*/
    if(nrmax>=n/2 +1)
    fout<<v[imax]<<" "<<nrmax;
    else
    fout<<-1;
    fin.close();
    fout.close();
    return 0;
}