Cod sursa(job #3285020)

Utilizator Cr1st1_5Diaconescu Cristian Alexandru Cr1st1_5 Data 12 martie 2025 14:08:28
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.86 kb
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,v[100001],q,i,x,y,ok,mij,st,dr,poz;
int main()
{
    fin>>n;
    for(i=1; i<=n; i++)
    {
        fin>>v[i];
    }
    fin>>q;
    for(i=1; i<=q; i++)
    {
        fin>>x>>y;
        if(x==0)
        {
            poz=0;
            ok=0;
            st=1;
            dr=n;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]==y)
                {
                    poz=mij;
                    ok=1;
                    st=mij+1;
                }
                else
                {
                    if(v[mij]>y)
                    {
                        dr=mij-1;
                    }
                    else
                    {
                        st=mij+1;
                    }
                }
            }
            if(ok)
            {
                fout<<poz<<endl;
            }
            else
            {
                fout<<-1<<endl;
            }
        }
        else
        {
            if(x==1)
            {
                poz=0;
                ok=0;
                st=1;
                dr=n;
                while(st<=dr)
                {
                    mij=(st+dr)/2;
                    if(v[mij]<=y)
                    {
                        poz=mij;
                        ok=1;
                        st=mij+1;
                    }
                    else
                    {
                        if(v[mij]>y)
                        {
                            dr=mij-1;
                        }
                    }
                }
                if(ok)
                {
                    fout<<poz<<endl;
                }
                else
                {
                    fout<<-1;
                }
            }
            else
            {

                    poz=0;
                    ok=0;
                    st=1;
                    dr=n;
                    while(st<=dr)
                    {
                        mij=(st+dr)/2;
                        if(v[mij]>=y)
                        {
                            poz=mij;
                            ok=1;
                            dr=mij-1;
                        }
                        else
                        {
                            if(v[mij]<y)
                            {
                                st=mij+1;
                            }
                        }
                    }
                    if(ok)
                    {
                        fout<<poz;
                    }
                    else
                    {
                        fout<<-1;
                    }
            }
        }
    }
    return 0;
}