Cod sursa(job #2429591)

Utilizator stefanut999Paul Colta stefanut999 Data 10 iunie 2019 13:41:46
Problema Elementul majoritar Scor 100
Compilator cpp-64 Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.73 kb
#include <iostream>
#include <cstring>
#include <stack>
#include <bits/stdc++.h>
#include <vector>
#define rep(a,b,c) for(a = b; a <= c; ++a)
#define nmax 1001
#define pob pop_back
#define pof pop_front
#define pub push_back
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");




int main()
{
  int i, n, x, cnt = 1, maj;
  fin >> n;
  int v[n + 1];
  fin >> v[1];
  x = v[1];
  maj = n / 2 + 1;
  rep(i, 2, n)
    {
      fin >> v[i];
      if(v[i] == x)
        {
          ++cnt;
        }
      else
        --cnt;
      if(cnt < 0)
        {
          x = v[i];
          cnt = 1;
        }
    }
  cnt = 0;
  rep(i, 1, n)
  {
    if(x == v[i])
      ++cnt;
  }
  if(cnt >= maj)
    fout << x<< " " << cnt;
  return 0;
}