Cod sursa(job #1209554)

Utilizator grayshadeLaurentiu Nicola grayshade Data 17 iulie 2014 23:52:00
Problema Elementul majoritar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    ifstream f("elmaj.in");
    vector<int> v;

    int votes = 0, candidate, n, k;
    f >> k;
    v.reserve(k);

    while (k-- > 0)
    {
        f >> n;
        v.push_back(n);

        if (votes == 0)
        {
            candidate = n;
            votes = 1;
        }
        else if (n == candidate)
            votes++;
        else
            votes--;
    }

    votes = 0;
    for (int i = 0; i < v.size(); i++)
        if (v[i] == candidate)
            votes++;

    ofstream of("elmaj.out");
    of << candidate << ' ' << votes << endl;
}