Cod sursa(job #1935932)

Utilizator ContDeRacistAliniateEBlat ContDeRacist Data 22 martie 2017 19:03:46
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>

using namespace std;

ifstream cin("cautbin.in");
ofstream cout("cautbin.out");
int v[100010],n;
/*bool exista(int poz,int x){
    for(int i=poz ; i<n ; ++i){
        if(v[i]==x){
            return true;
        }
    }
    return false;
}
bool existabet(int poz,int x){
    for()}*/
int c0(int x)
{
    int pas=1<<16,r=-1;
    while(pas!=0)
    {
        if(r+pas<=n && v[r+pas]<=x)
        {
            r+=pas;
        }
        pas/=2;
    }
    if (v[r] != x)
    {
        return -1;
    }
    return r;
}
int c1(int x)
{
    int pas=1<<16,r=-1;
    while(pas!=0)
    {
        if(r+pas<=n && v[r+pas]<=x)
        {
            r+=pas;
        }
        pas/=2;
    }
    return r;
}
int c2(int x)
{
    int pas=1<<16,r=-1;
    while(pas!=0)
    {
        if(r+pas<=n && v[r+pas]<x)
        {
            r+=pas;
        }
        pas /= 2;
    }
    return 1 + r;
}
int main()
{
    int m,cer,nr;
    cin>>n;
    for(int i=1 ; i<=n ; ++i)
    {
        cin>>v[i];
    }
    cin>>m;
    for(int i=0 ; i<m ; ++i)
    {
        cin>>cer>>nr;
        if(cer==0)
        {
            cout<<c0(nr);
        }
        else if(cer==1)
        {
            cout<<c1(nr);
        }
        else if(cer==2)
        {
            cout<<c2(nr);
        }
        cout << "\n";
    }
    return 0;
}