Cod sursa(job #2679757)

Utilizator starduststardust stardust Data 1 decembrie 2020 14:08:03
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <fstream>
#include <vector>
using namespace std;

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

int main()
{
    int n;
    vector<int> nums;
    int count = 1;
    in >> n;
    int x;
    in >> x;
    int elmaj = x;
    nums.push_back(x);
    for (int i = 1; i < n; i++)
    {
        in >> x;
        if (x == elmaj)
            count++;
        else
        {
            count--;
            if (count == 0)
            {
                elmaj = x;
                count = 1;
            }
        }
        nums.push_back(x);
    }

    count = 0;
    for (int i = 0; i < n; i++)
    {
        if (nums[i] == elmaj)
            count++;
    }

    if (count < n / 2 + 1)
        out << "-1";
    else
        out << elmaj << " " << count;
}