Cod sursa(job #1669876)

Utilizator hegedusPaul Hegedus hegedus Data 31 martie 2016 10:37:33
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <cstdio>
#include <algorithm>
using namespace std;

const int nmax=1000000;
int v[nmax],n,x,y1,y2,k,mij;

void citire()
{
    freopen("elmaj.in","r",stdin);
    freopen("elmaj.out","w",stdout);
    scanf("%d",&n);
    for(x=1;x<=n;x++)
        scanf("%d",&v[x]);
}

int main()
{
    citire();
    sort(v+1,v+n+1);
    mij=(n+1)/2;
    x=v[mij];
    y1=mij-1;
    y2=mij+1;
    k=1;
    do
    {
        if (x==v[y1]) y1--, k++;
        if (x==v[y2]) y2++, k++;
    }while (x==v[y1] || x==v[y2]);
    if (k>n/2) printf("%d %d\n",x,k);
    else printf("-1\n");
    return 0;
}