Cod sursa(job #760582)

Utilizator bratualexBratu Alexandru bratualex Data 22 iunie 2012 01:08:10
Problema Elementul majoritar Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 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,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]>=n/2 +1)
        {
            fout<<v[j]<<" "<<f[j];
            fin.close();
            fout.close();
            return 0;
        }
    }
    //if(nrmax>=n/2 +1)
    //fout<<v[imax]<<" "<<nrmax;
    //else
    fout<<-1;
    fin.close();
    fout.close();
    return 0;
}