Cod sursa(job #2427819)

Utilizator TheSHAD0WLepadatu Vlad-Tudor TheSHAD0W Data 2 iunie 2019 13:02:31
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <fstream>
using namespace std;
ifstream in("cautbin.in");
ofstream out("cautbin.out");

int v[100001];

int cb0(int st, int dr, int x)
{
    int mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(v[mij]<=x)
            st=mij+1;
        else
            dr=mij-1;
    }
    mij=(st+dr)/2;
    if(v[mij]>x)
        mij--;
    if(v[mij]==x)
        return mij;
    return -1;
}

int cb1(int st, int dr, int x)
{
    int mij,n=dr;
    while(st<dr)
    {
        mij=(st+dr)/2;
        if(v[mij]<=x)
            st=mij+1;
        else
            dr=mij;
    }
    mij=(st+dr)/2;
    if(v[mij]>x)
        mij--;
    return mij;
}

int cb2(int st, int dr, int x)
{
    int mij;
    while(st<dr)
    {
        mij=(st+dr)/2;
        if(v[mij]<x)
            st=mij+1;
        else
            dr=mij;
    }
    mij=(st+dr)/2;
    if(v[mij]<x)
        mij++;
    return mij;
}

int main()
{
    int i,n,m,x,y;
    in>>n;
    for(i=1;i<=n;i++)
        in>>v[i];
    in>>m;
    while(m--)
    {
        in>>x>>y;
        if(x==0)
            out<<cb0(1,n,y)<<'\n';
        if(x==1)
            out<<cb1(1,n,y)<<'\n';
        if(x==2)
            out<<cb2(1,n,y)<<'\n';
    }
    return 0;
}