Cod sursa(job #2251773)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 1 octombrie 2018 22:28:48
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <vector>
#define cout fout
using namespace std;
ifstream fin ("elmaj.in");
ofstream fout ("elmaj.out");
vector<int> v;
int n, x, maj, ap, i;
int main()
{
    fin >> n;
    for(i=0; i<n; i++)
    {
        fin >> x;
        v.push_back(x);
    }
    maj = v[0];
    ap = 1;
    for(i=1; i<n; i++)
    {
        if(v[i] == maj)
            ap++;
        else ap--;
        if(!ap)
        {
            ap = 1;
            maj = v[i];
        }
    }
    ap = 0;
    for(i=0; i<n; i++)
        if(v[i] == maj)
            ap++;
    if(ap >= n/2+1)
        cout << maj << ' ' << ap;
    else cout << -1;

    return 0;
}