Cod sursa(job #1065540)

Utilizator krissu93FMI Tiugan Cristiana Elena krissu93 Data 23 decembrie 2013 14:23:21
Problema Elementul majoritar Scor 0
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.96 kb
//#include <iostream>
#include <fstream>
#include <vector>
#include <utility>

using namespace std;

const int MOD =  666013;
int n;
int el_maj, ap_max=0;

vector <pair <int, int> > hash[MOD];

inline void update(int val, int ap){
  if (ap>ap_max){
    ap_max = ap;
    el_maj = val;
  }
}
int in_hash(int val, int key){
  int sz = hash[key].size();
  for (int i = 0; i < sz; ++i)
    if (hash[key][i].first == val)
      return i;
  return -1;
}
void put_output(){
  ofstream out("elmaj.out");
  //cout<<ap_max;
  if ( ap_max >= n/2+1 ) out<<el_maj<<" "<<ap_max;
  else out<<-1;
  out.close();
}

int main(){
  ifstream in("elmaj.in");
  in>>n;

  for (int i = 1; i<= n; ++i){
    int val;
    in>>val;
    int key = val % MOD;
    int poz = in_hash(val,key);

    if (poz==-1){
      hash[key].push_back(make_pair(val,1));
      update(val,1);
    }else{
      hash[key][poz].second++;
      update(hash[key][poz].first,hash[key][poz].second);
    }
  }
  in.close();


  return 0;


}