Cod sursa(job #1321049)

Utilizator ispikeAlex Bojan ispike Data 18 ianuarie 2015 19:05:36
Problema Cautare binara Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>
//#include <iostream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int i,val,n,x,stop,a[100],m,y,j;
int binary_searchh(int val)
{
    int i, step;
    for (step = 1; step < n; step <<= 1);
    for (i = 0; step; step >>= 1)
        if (i + step < n && a[i + step] <= val)
           i += step;
    return i;
}
int binary_searchhh(int val)
{
    int i, step;
    for (step = 1; step < n; step <<= 1);
    for (i = 0; step; step >>= 1)
        if (i + step < n && a[i + step] < val)
           i += step;
    return i+1;
}
int main()
{
    fin>>n;
    for(i=1;i<=n;++i)
        fin>>a[i];
    fin>>m;
    i=1;
    while(i<=m)
    {
        fin>>y>>x;
        if(y==0)
        {
            if(a[binary_searchh(x)]==x)
                fout<<binary_searchh(x)<<"\n";
                else
                fout<<-1<<"\n";
        }
        if(y==1)
            fout<<binary_searchh(x)<<"\n";
        if(y==2)
        {
            //if(a[binary_searchhh(x)]<=x)
                fout<<binary_searchhh(x)<<"\n";
               // else
                //fout<<-1<<"\n";
        }
        i++;

    }

    return 0;
}