Cod sursa(job #1493763)

Utilizator ghimbirChirica Cristian ghimbir Data 29 septembrie 2015 21:13:43
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("cautbin.in");
ofstream g("cautbin.out");

const int Vmax=100;
int v[Vmax];

int main()
{
    int n,q;
    f>>n;
    for(int i=1; i<=n; i++)
        f>>v[i];
    f>>q;

    int lo,hi,mid,x,pos,vi;
    for(int i=1; i<=q; i++)
    {

        f>>vi>>x;
        lo=1;
        hi=n;
        if(vi==0)
        {


            while(lo<=hi)
            {
                mid=(lo+hi)/2;
                if(v[mid]==x)
                {
                    pos=mid;
                }
                if(v[mid]>x)
                    hi=mid-1;
                else
                    lo=mid+1;

            }


        g<<pos<<"\n";
        }

         if(vi==1)
        {


            while(lo<=hi)
            {
                mid=(lo+hi)/2;
                if(v[mid]<=x)
                {
                    pos=mid;
                }
                if(v[mid]>x)
                    hi=mid-1;
                else
                    lo=mid+1;

            }


        g<<pos<<"\n";
        }

         if(vi==2)
        {


            while(lo<=hi)
            {
                mid=(lo+hi)/2;
                if(v[mid]>=x)
                {
                    pos=mid;
                    hi=mid-1;
                }
                if(v[mid]==x)
                    hi=mid-1;
                else
                    lo=mid+1;

            }


        g<<pos<<"\n";
        }
    }



return 0;
}