Cod sursa(job #419955)

Utilizator biroBiro Alexandru biro Data 18 martie 2010 11:21:53
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include<algorithm>
#define input "back.in"
#define output "back.out"
#define DIM 1100

using namespace std ;

int n ;
int x ;
int v[DIM] ;

void read()
    {
        scanf ("%d%d" , &n , &x) ;  
        for (int i=1 ; i<=n ; ++i)
            {
                scanf ("%d" , &v[i]) ;   
            }  
    }
int solve()
    {
        int st=1 ;
        int dr=n ;
        int gasit=0 ;
        int mijloc ;
        while (st<=dr)
            {
                mijloc=(st+dr)>>1 ;
                if (v[mijloc]==x) 
                    {
                        printf ("%d" , mijloc) ;
                        return 0;   
                    }   
                else
                    {
                        if (x>v[mijloc])
                            {
                                st=mijloc+1 ;
                            }
                        else
                            {
                                dr=mijloc-1 ;    
                            }
                    }
            }    
        return -1 ;    
    }
    
int main()
{
    freopen (input,"r",stdin) ;
    freopen (output,"w",stdout) ;
    
    read() ;
    solve() ;
 
}