Cod sursa(job #2708720)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 19 februarie 2021 11:39:51
Problema Elementul majoritar Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb

/******************************************************************************
{1}
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
{1}
*******************************************************************************/
#include <stdio.h>
#include <iostream>
#include <map>

using namespace std;

map <int,int>m;

int main()
{
    freopen("elmaj.in","r",stdin);
    freopen("elmaj.out","w",stdout);
    int n,nr,c=0,cc=-1;
    scanf("%i",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%i",&nr);
        m[nr]++;
        if(cc==-1)
        {
            cc=nr;
        }
        if(cc==nr) c++;
        if(cc!=nr) c--;
        if(c<0)
        {
            c=-c;
            cc=nr;
        }
    }
    if(c<1||m[cc]<(n/2+1)) printf("-1");
    else printf("%i %i",cc,m[cc]);
    return 0;
}