Cod sursa(job #2377747)

Utilizator dan.ghitaDan Ghita dan.ghita Data 11 martie 2019 02:19:29
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("elmaj.in");
ofstream g("elmaj.out");

long long n, x, maj, cnt;
vector<long long> v;

int main()
{
    f >> n;

    for(int i = 0; i < n; ++i)
    {
        f >> x;
        v.push_back(x);

        if (cnt == 0)
            maj = x,
            cnt = 1;
        else
            if (maj == x)
                ++cnt;
            else
                --cnt;
    }

    long long totalCnt = count_if(v.begin(), v.end(), [](long long x) { return x == maj; });

    if (totalCnt > n / 2)
        g << maj << ' ' << totalCnt;
    else
        cout << -1;

    return 0;
}