Cod sursa(job #1990668)

Utilizator cosmo0093Raduta Cosmin cosmo0093 Data 12 iunie 2017 22:31:32
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>

int main() {
    std::ifstream fileIn("elmaj.in");
    std::ofstream fileOut("elmaj.out");

    int nV;
    fileIn >> nV;

    std::vector<int> myV(nV);

    int aux, vCount;
    for (int i(0); i < nV; i++) {
        fileIn >> myV[i];
        if (!vCount) {
            aux = myV[i];
        }
        if (aux == myV[i]) {
            vCount++;
        } else {
            vCount--;
        }
    }

    vCount = 0;
    for (int val : myV) {
        if (val == aux) {
            vCount++;
        }
    }

    if (vCount > nV / 2) {
        fileOut << aux << ' ' << vCount;
    } else {
        fileOut << -1;
    }

    fileIn.close();
    fileOut.close();
    return 0;
}