Cod sursa(job #1987670)

Utilizator Moise_AndreiMoise Andrei Moise_Andrei Data 31 mai 2017 17:19:08
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("elmaj.in");
ofstream out("elmaj.out");
int v[1000005];
int main()
{
    int n;
    in >> n >> v[1];
    int c = 1;
    int t = v[1];
    for(int i = 2; i <= n; i++)
    {
        in >> v[i];
        if(t != v[i])
            c--;
        else
            c++;
        if(c == 0)
            t = v[i], c = 1;
    }
    if(c > 0)
    {
        c = 0;
        for(int i = 1; i <= n; i++)
            if(t == v[i])
                c++;
        if(c >= n / 2 + 1)
            out << t << " " << c;
        else
            out << "-1";
        return 0;
    }
    out << "-1";
    return 0;
}