Cod sursa(job #2646834)

Utilizator nandemaNema Dan-Cristian nandema Data 2 septembrie 2020 10:14:38
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>

using namespace std;

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

int n, v[100010];

int cb(int a, int b)
{
    int loc, step;
    for(step = 1; step<=n; step<<=1);
    if(a==0)
    {
        for(loc = 1; step; step>>=1)
        {
            if(loc+step <= n && v[loc+step] <= b)
                loc+=step;
        }
        if(loc) return loc;
        else return -1;
    }
    if(a==1)
    {
        for(loc = 1; step; step >>= 1)
            if(loc+step <= n && v[loc+step] <= b)
                loc+=step;
        return loc;
    }
    if(a==2)
    {
        for(loc = n; step; step>>=1)
            if(loc-step >0 &&v[loc-step] >= b)
                loc-=step;
        return loc;
    }
}

int main()
{
    f>>n;
    for(int i=1;i<=n;++i)
        f>>v[i];
    int t;
    f>>t;
    int a, b;
    for(int i=0; i<t; ++i)
    {
        f>>a>>b;
        g<<cb(a,b)<<'\n';
    }
}