Cod sursa(job #3282576)

Utilizator BogdanBurescuBogdan Burescu BogdanBurescu Data 6 martie 2025 08:26:11
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>

using namespace std;

ifstream cin ("cautbin.in");
ofstream cout ("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()
{
   cin>>n;
    for(int i=1;i<=n;i++)
        cin>>v[i];
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>cer>>x;
        if(cer==0)
            cautare0(x);
        if(cer==1)
            cautare1(x);
        if(cer==2)
            cautare2(x);
         cout<<poz<<'\n';
    }
    return 0;
}