Cod sursa(job #1327519)

Utilizator AlphakingBorea Vlad Gabriel Alphaking Data 26 ianuarie 2015 20:03:49
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include<iostream>
#include<fstream>
using namespace std ;
ifstream fin ( "cautbin.in" ) ;
ofstream fout ( "cautbin.out" ) ;
int n , i , v[100003] ;
int cautbin01 ( int x )
{
    int m , mij , s=1 , d=n ;
    while ( s<=d )
    {
        mij=(s+d)/2 ;
        if ( v[mij]<=x )
            s=mij+1 ;
        else
            d=mij-1 ;
    }
    return d ;
}
int cautbin2 ( int x )
{
    int m , mij , s=1 , d=n ;
    while ( s<=d )
    {
        mij=(s+d)/2 ;
        if ( v[mij]<x )
            s=mij+1 ;
        else
            d=mij-1 ;
    }
    return s ;
}
int main ()
{
    int m , a , x , poz ;
    fin>>n ;
    for ( i=1 ; i<=n ; i++ )
        fin>>v[i] ;
    fin>>m ;
    for ( i=1 ; i<=m ; i++ )
    {
        fin>>a>>x ;
        if ( a==0 or a==1 )
        {
            poz= cautbin01 ( x ) ;
            if ( a==0 and v[poz]!=x )
                fout<<-1<<'\n' ;
            else
                fout<<poz<<'\n' ;
        }
        else
            fout<< cautbin2 ( x ) <<'\n' ;
    }
    return 0 ;
}