Cod sursa(job #1721330)

Utilizator LittleWhoFeraru Mihail LittleWho Data 25 iunie 2016 12:32:20
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    ifstream in("cautbin.in");
    ofstream out("cautbin.out");
    
    int n, m;
    in >> n;
    
    vector<int> v;
    int xmin = 0, xmax = 0;
    for (int i = 0; i < n; i++) {
        int nr;
        in >> nr;
        v.push_back(nr);
        if (xmin > nr) xmin = nr;
        if (xmax < nr) xmax = nr;
    }
    sort(v.begin(), v.end());
    
    in >> m;
    for (int i = 0; i < m; i++) {
        int intg, x;
        in >> intg >> x;
        
        if (intg == 0) {
            vector<int>::iterator up = upper_bound(v.begin(), v.end(), x);
            if (up - v.begin() > 0 && up - v.begin() < n)
                out << up - v.begin() << "\n";
            else
                out << "-1" << "\n";
        } else if (intg == 1) {
            vector<int>::iterator up = upper_bound(v.begin(), v.end(), x + 1);
            out << up - v.begin() << "\n";
        } else if (intg == 2) {
            vector<int>::iterator low = lower_bound(v.begin(), v.end(), x - 1);
            out << low - v.begin() + 1 << "\n";
        }
    }
    
    in.close();
    out.close();
    return 0;
}