Cod sursa(job #896118)

Utilizator ocolisanrvocolisan ocolisanrv Data 27 februarie 2013 13:54:21
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include<stdio.h>
#include <algorithm>
FILE *in,*out;
int n,i,j,v[1000001],aux,maj,mij,contor,contfinal;
int main()
{
    in=fopen("elmaj.in","rt");
    out=fopen("elmaj.out","wt");
    fscanf(in,"%d",&n);
    for(i=1;i<=n;i++)
        fscanf(in,"%d",&v[i]);
    /*for(i=1;i<n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(v[i]>v[j])
            {
                aux=v[i];
                v[i]=v[j];
                v[j]=aux;
            }
        }
    }*/
    std::sort(v+1, v+n+1);
    mij = n/2+1;
    maj=v[mij];
    while(v[mij--]==maj)
        contor++;
    mij=(n/2)+1;
    while(v[mij++]==maj)
        contor++;
    contfinal=contor-1;
    fprintf(out,"%d %d",maj,contfinal);
    fclose(in);
    fclose(out);
    return 0;
}