Cod sursa(job #1723495)

Utilizator CipiNisNisioi Ciprian CipiNis Data 30 iunie 2016 19:26:49
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <algorithm>
#define DIMMAX 100100

using namespace std;

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

int V[DIMMAX];

int main()
{
    int n, m, x, y, i, t;

    f >> n;

    for (i = 1; i <= n; ++i)
        f >> V[i];

    sort(V + 1, V + n + 1);

    f >> m;

    for(i = 1; i <= m; ++i){
        f >> t >> y;

        if (t == 0){
            x = upper_bound(V + 1, V + n + 1, y) - V - 1;
            if (x <= n && x >= 1 && V[x] == y)
                g << x << "\n";
            else g << -1 << "\n";
        }
        else if(t == 1){
            x = lower_bound(V + 1, V + n + 1, y + 1) - V - 1;
            g << x << "\n";
        }
        else{
            x = upper_bound(V + 1, V + n + 1, y - 1) - V;
            g << x << "\n";
        }
    }
    return 0;
}