Cod sursa(job #2692533)

Utilizator IuliaDamianDamian Iulia Alexandra IuliaDamian Data 2 ianuarie 2021 22:43:06
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream fin("date.in");

int main() {
    int n, v[101], t, y, m;
    fin >> n;
    for (int i =1; i <= n; ++i) {
        fin >> v[i];
    }
    sort(v + 1, v + n + 1);
    fin >> m;
    for (int i = 1; i <= m; ++i) {
        fin >> t >> y;
        if (t == 0) {
            int x = upper_bound(v + 1, v + n + 1, y) - v - 1;
            if (x <= n && x >= 1 && v[x] == y) {
                cout << x << "\n";
            } else {
                cout << "-1\n";
            }
        } else if (t == 1) {
            int x = lower_bound(v + 1, v + n+ 1, y + 1) - v - 1;
            cout << x << "\n";
        } else {
            int x = upper_bound(v + 1, v + n + 1, y - 1) - v;
            cout << x << "\n";
        }
    }
}