Cod sursa(job #3176757)

Utilizator mili_taruMilitaru Razvan Ionut mili_taru Data 27 noiembrie 2023 18:40:18
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.85 kb
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,m,v[100001],nrintrb,nr,st,dr,mij,poz,maxx,i,x;
int main()
{
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>v[i];
    }
    fin>>m;
    for(i=1;i<=m;i++)
    {
        fin>>nrintrb>>x;
        if(nrintrb==0)
        {
            poz=-1;
            st=1;
            dr=n;
            while(st<=dr && poz==-1)
            {
                mij=(st+dr)/2;
                if(v[mij]==x)
                {
                    poz=mij;
                    st=mij+1;
                }
                else
                {
                    if(v[mij]<x)
                    {
                        st=mij+1;
                    }
                    else
                    {
                        dr=mij-1;
                    }
                }
            }
            fout<<poz<<endl;
        }
        if(nrintrb==1)
        {
            st=1;
            dr=n;
            poz=-1;
            maxx=0;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]<=x)
                {
                    poz=mij;
                    st=mij+1;
                }
                else
                {
                    dr=mij-1;
                }
            }
            fout<<poz<<endl;
        }
        if(nrintrb==2)
        {
            st=1;
            dr=n;
            poz=-1;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>=x)
                {
                    poz=mij;
                    dr=mij-1;
                }
                else
                {
                    st=mij+1;
                }
            }
            fout<<poz<<endl;
        }
    }
    return 0;
}