Cod sursa(job #1806976)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 15 noiembrie 2016 21:18:39
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <map>
#include <cstdio>
#include <cstdlib>
using namespace std;

int a[1000001],n;
map<int,int>m;

bool isdigit(char c)
{
    return c>='0' && c<='9';
}

FILE * pFile;
long lSize;
char * buffer;
size_t result;

void readfile()
{


    pFile = fopen ( "elmaj.in" , "rb" );
    fseek (pFile , 0 , SEEK_END);
    lSize = ftell (pFile);
    rewind (pFile);

    buffer = (char*) malloc (sizeof(char)*lSize);

    result = fread (buffer,1,lSize,pFile);

    fclose (pFile);
   // free (buffer);
}

int pos=0;

int readint()
{
    int x=0,sgn=1;
    while(buffer[pos]!=' ' && buffer[pos]!='\n' && pos<lSize*sizeof(char))
    {
        if(buffer[pos]=='-')
            sgn=-1;
        if(isdigit(buffer[pos]))
            x=x*10+(buffer[pos]-'0');

        ++pos;
    }
    while(!isdigit(buffer[pos]) && pos<lSize*sizeof(char)) ++pos;
    return x*sgn;
}

void citire()
{
    readfile();
    n=readint();
    for(int i=0;i<n;i++)
    {
        a[i]=readint();
        m[a[i]]++;
    }

}

void solve()
{
    int ma,me;
    for(int i=0;i<n;i++)
    {
        if(m[a[i]]>ma)
        {
            ma=m[a[i]];
            me=a[i];
        }
    }
    fprintf(fopen("elmaj.out","w"),"%d %d",ma,me);
}

int main()
{

    citire();

    solve();

    return 0;
}