Cod sursa(job #3245900)

Utilizator stefanpiturStefan Alexandru Pitur stefanpitur Data 1 octombrie 2024 00:25:27
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
/******************************************************************************

Welcome to GDB Online.
  GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
  C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    FILE *fin, *fout;
    fin = fopen("elmaj.in", "r");
    fout = fopen("elmaj.out", "w");
    
    int n, cnt, maj, x;
    cnt = 1;
    fscanf(fin, "%d %d", &n, &maj);
    
    vector<int> v(n);
    v[0] = maj;
    
    for (int i = 1; i < n; i++) {
        fscanf(fin, "%d", &x);
        v[i] = x;
        if (maj == x) {
            cnt++;
            continue;
        }
        
        cnt--;
        if (cnt == 0) {
            maj = x;
            cnt = 1;
        }
    }
    
    cnt = 0;
    for (int i = 0; i < n; i++)
        cnt += (maj == v[i]);
    fprintf(fout, "%d %d\n", maj, cnt);
    fclose(fin);
    fclose(fout);
    return 0;
}