Cod sursa(job #2675938)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 22 noiembrie 2020 22:32:59
Problema Elementul majoritar Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
/******************************************************************************

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.

*******************************************************************************/
#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;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>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)) cout<<-1;
    else cout<<cc<<' '<<m[cc];
    return 0;
}