Cod sursa(job #1727428)

Utilizator jurjstyleJurj Andrei jurjstyle Data 10 iulie 2016 19:18:33
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <fstream>
#include <iostream>

using namespace std ;

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

int v[100005] , n , m ;

int cautare_binara_0 ( int x )
{
 int st = 1 , dr = n , mij ;
 while ( st <= dr )
    {
     mij = ( st + dr ) / 2 ;
     if ( v[mij] > x )
        dr = mij - 1 ;
     else
        st = mij + 1 ;
    }
 mij = ( st + dr ) / 2 ;
 if ( v[mij] > x )
    --mij ;
 if ( v[mij] != x )
    return -1 ;
 return mij ;
}

int cautare_binara_1 ( int x )
{
 int st = 1 , dr = n , mij ;
 while ( st < dr )
    {
     mij = ( st + dr ) / 2 ;
     if ( v[mij] > x )
        dr = mij ;
     else
        st = mij + 1 ;
    }
 mij = ( st + dr ) / 2 ;
 if ( v[mij] > x )
    --mij ;
 return mij ;
}

int cautare_binara_2 ( int x )
{
 int st = 1 , dr = n , mij ;
 while ( st < dr )
    {
     mij = ( st + dr ) / 2 ;
     if ( v[mij] >= x )
        dr = mij ;
     else
        st = mij + 1 ;
    }
 mij = ( st + dr ) / 2 ;
 if ( v[mij] < x )
    ++mij ;
 return mij ;
}


int main ()
{
 f >> n ;
 for ( int i = 1 ; i <= n ; ++i )
    f >> v[i] ;
 f >> m ;
 for ( ; m ; --m )
    {
     int tip , nr ;
     f >> tip >> nr ;
     if ( tip == 0 )
        g << cautare_binara_0 ( nr ) << "\n" ;
     if ( tip == 1 )
        g << cautare_binara_1 ( nr ) << "\n" ;
     if ( tip == 2 )
        g << cautare_binara_2 ( nr ) << "\n" ;

    }
}