Cod sursa(job #2669362)

Utilizator claudiuhamciucHamciuc Claudiud claudiuhamciuc Data 6 noiembrie 2020 20:06:48
Problema Cautare binara Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
using namespace std;

ifstream fin("cautbin.in");
ofstream fout("cautbin.out");

#define nmax 100005
int n, m, st, dr, mij, poz, cer, x;
int v[nmax];


void cautare0(int x){
    poz=-1;
    st=1; dr=n;
    while (st<=dr){
        mij=(st+dr)/2;
        if (v[mij]==x)
            poz=mij;
        if (v[mij]<=x)
            st = mij+1;
        else
            dr = mij-1;
    }
}

void cautare1(int x){
    poz=-1;
    st=1; dr=n;
    while (st<=dr){
        mij=(st+dr)/2;
        if (v[mij]<=x){
            st = mij+1;
            poz = mij;
        }
        else
            dr = mij-1;
    }
}

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

int main()
{
    int i;
    fin>>n;
    for(i=1;i<=n;i++)
        fin>>v[i];
    fin>>m;
    for (int i=1;i<=m;i++){
        fin>>cer>>x;
        if (cer==0)
            cautare0(x);
        if (cer==1)
            cautare1(x);
        if (cer==2)
            cautare2(x);
        fout<<poz<<endl;
    }
    return 0;
}