Cod sursa(job #2416877)

Utilizator ela_topaTopa Elena ela_topa Data 28 aprilie 2019 14:12:08
Problema Cautare binara Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
//cautare binara cu upper si lower
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
constexpr int NX = 100010;
int v[NX];
int main()
{
    int N, M, x, y;
    fin>>N;
    for(int i=1; i<=N; ++i)
    fin>>v[i];
    fin>>M;
    for(int i=0; i<M; ++i)
    {
        fin>>x>>y;
        if(x==0)
        {
            int z=upper_bound(v+1, v+N+1, y)-v-1;
                if(z<=N && z>=1 && v[z]==y)
                    fout<<z<<"\n";
                else
                    fout<<"-1\n";
        }
        else if(x==1)
        {
            fout<<lower_bound(v+1, v+N+1, y+1)-v-1<<"\n";
        }
        else if(x==2)
        {
            fout<<lower_bound(v+1, v+N+1, y-1)-v<<"\n";
        }
    }
    return 0;

}