Cod sursa(job #642549)

Utilizator bmaticanBogdan-Alexandru Matican bmatican Data 1 decembrie 2011 17:04:03
Problema Cautare binara Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <complex>
#include <algorithm>

using namespace std;

ifstream in("cautbin.in");
ofstream out("cautbin.out");

int n;
vector<int> v(10, 0);

void solve() {
  in >> n;
  v.resize(n, 0);
  v[0] = -1;
  int temp;

  for (int i = 1; i <= n; ++i) {
    in >> temp;
    v[i] = temp;
  }

  sort(v.begin(), v.end());

  int m;
  in >> m;
  int x, y;
  while (m--) {
    in >> x >> y;
    vector<int>::iterator it;
    switch(x) {
      case 0:
        it = upper_bound(v.begin(), v.end(), y);
        --it;
        if (*it == y) {
          out << (it - v.begin()) << endl;
        } else {
          out << -1 << endl;
        }
        break;
      case 1:
        it = lower_bound(v.begin(), v.end(), y + 1);
        out << (--it - v.begin()) << endl;
        break;
      case 2:
        it = upper_bound(v.begin(), v.end(), y - 1);
        out << (it - v.begin()) << endl;
        break;
    }
  }
}

int main() {
  solve();
  return 0;
}