Cod sursa(job #3166639)

Utilizator MogoneaMIhneaMogonea Mihnea Mihai MogoneaMIhnea Data 9 noiembrie 2023 10:13:43
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n, i, j, x, st, dr, mid, v[10001], aux, m, q;
int main(){
    fin>>n;
    for(i=1;i<=n;i++)
        fin>>v[i];
    fin>>m;
    for(i=1;i<=m;i++){
        fin>>q>>x;
        if(q==0){
            st=1;
            dr=n;
            while (st<=dr){
                mid=(st+dr)/2;
                if(v[mid]==x){
                    aux=mid;
                    st=mid+1;
                }
                if(x>=v[mid])
                    st=mid+1;
                else
                    dr=mid-1;
            }
            if(aux!=0)
                fout<<aux<<"\n";
                else
                    fout<<"-1";

            }
            if(q==1){
                st=1;
                dr=n;
                while (st<=dr){
                    mid=(st+dr)/2;
                    if(v[mid]<=x){
                        aux=mid;
                        st=mid+1;
                    }
                    if(x>=v[mid])
                        st=mid+1;
                    else
                        dr=mid-1;
                }
                if(aux!=0)
                    fout<<aux<<"\n";

            }
            if(q==2){
                st=1;
                dr=n;
                while (st<=dr){
                    mid=(st+dr)/2;
                    if(v[mid]>=x){
                        aux=mid;
                        dr=mid-1;
                    }else
                        st=mid+1;

                }
                if(aux!=0)
                    fout<<aux<<"\n";

            }
    }
}