Pagini recente » Cod sursa (job #596895) | Cod sursa (job #1549180) | Cod sursa (job #83051) | Cod sursa (job #2871146) | Cod sursa (job #2278121)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in") ;
ofstream g("cautbin.out") ;
int n , m , a[100002] , x , ok ;
void binar( int st , int dr )
{ if( st <= dr ){
int poz = ( st + dr ) / 2 ;
if( a[poz] == x && a[poz+1] != x ){
g << poz << "\n" ;
ok = 0 ;
}
else
if( a[poz] <= x )
binar( poz + 1 , dr ) ;
else
binar( st , poz - 1 ) ;
}
}
void binar1( int st , int dr )
{ if( st <= dr ){
int poz = ( st + dr ) / 2 ;
if( a[poz] <= x && a[poz+1] > x ){
g << poz << "\n" ;
}
else
if( a[poz] <= x )
binar( poz + 1 , dr ) ;
else
binar( st , poz - 1 ) ;
}
}
void binar2( int st , int dr )
{ if( st <= dr ){
int poz = ( st + dr ) / 2 ;
if( a[poz] >= x && a[poz-1] < x ){
g << poz << "\n" ;
}
else
if( a[poz] <= x )
binar( poz + 1 , dr ) ;
else
binar( st , poz - 1 ) ;
}
}
int main()
{
f >> n ;
for( int i = 1 ; i <= n ; i ++ )
f >> a[i] ;
f >> m ;
for( int i = 1 ; i <= m ; i ++ )
{ int operatie ;
f >> operatie >> x ;
ok = 1 ;
if( operatie == 0 ){
binar( 1 , n ) ;
if( ok )
g << "-1\n" ;
}
if( operatie == 1 )
binar1( 1 , n ) ;
if( operatie == 2 )
binar2( 1 , n ) ;
}
}