Cod sursa(job #2186997)

Utilizator OldpugAlex Ionescu Oldpug Data 26 martie 2018 09:35:54
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda bpc10 Marime 0.64 kb
#include <algorithm>
#include <fstream>
#include <functional>
#include <map>
#include <cstdint>

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

  std::map<int32_t, int32_t, std::greater<int32_t>> map;

  int32_t n{};
  in >> n;

  int32_t i{};
  while (in >> i)
    ++map[i];

  std::sort(map.begin(), map.end(), [](const std::pair<int32_t, int32_t> &lhs, const std::pair<int32_t, int32_t> &rhs){ return lhs.second > rhs.second; });

  auto fst{map.begin()};
  auto elem{fst->first};
  auto count{fst->second};

  if (count > n / 2)
    out << elem << ' ' << count;
  else
    out << -1;
}