Cod sursa(job #2073784)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 23 noiembrie 2017 18:39:23
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include<fstream>
using namespace std;
int v[100001];
int cautb0(int x, int n)
{
    int position=0;
    for(int power=20; power>=0; --power)
        if(position+(1<<power)<=x && v[position+(1<<power)]<=n)
            position+=1<<power;
    return position;
}
int main()
{
    ifstream fin("cautbin.in");
    ofstream fout("cautbin.out");
    int N, M;
    fin >> N;
    for(int i=1; i<=N; i++)
        fin >> v[i];
    fin >> M;
    for(int i=1; i<=M; ++i)
    {
        int type, x;
        fin >> type >> x;
        if(type==0)
            if(v[cautb0(N,x)]!=x)
                fout << "-1" << '\n';
            else fout << cautb0(N, x) << '\n';
        if(type==1)
            fout << cautb0(N, x) << '\n';
        if(type==2)
            fout << cautb0(N, x-1)+1 << '\n';
    }
    return 0;
}