Cod sursa(job #760579)

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

using namespace std;
const int N=1000001;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int main()
{
    int n,check,j,i,k=0,nrmax=0,imax=0,v[N],x,f[N];
    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;
}