Cod sursa(job #743058)

Utilizator GagosGagos Radu Vasile Gagos Data 2 mai 2012 23:14:21
Problema Elementul majoritar Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <fstream>
#include <set>
#include <vector>

#define mp make_pair

using namespace std;

class ComparePairs {
 public:
  bool operator() (const pair<int, int>& a, const pair<int, int>& b) const {
    return a.first < b.first;
  }
};

int main() {
  bool ok = false;
  int size;
  int i;
  int val;
  int old_count;
  ifstream in_file("elmaj.in");
  ofstream out_file("elmaj.out");
  set<pair<int, int>, ComparePairs> aset;
  set<pair<int, int>, ComparePairs>::iterator it;

  in_file >> size;

  for (i = 0; i < size; ++i) {
    in_file >> val;

    if ((it = aset.find(mp(val, 0))) != aset.end()) {
      old_count = (*it).second;

      aset.erase(it);
      aset.insert(mp(val, old_count + 1));
    } else {
      aset.insert(mp(val, 1));
    }
  }

  size >>= 1;

  for (it = aset.begin(); it != aset.end(); ++it) {
    if ((*it).second > size) {
      ok = true;

      break;
    }
  }

  if (ok == true) {
    out_file << (*it).first << " " << (*it).second;
  } else {
    out_file << "-1";
  }

  in_file.close();
  out_file.close();

  return 0;
}