Cod sursa(job #420013)

Utilizator biroBiro Alexandru biro Data 18 martie 2010 12:37:49
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.9 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" , &n ) ; 
        for (int i=1 ; i<=n ; ++i)
            {
                scanf ("%d" , &v[i]) ;  
            } 
        scanf ("%d" , &x ) ; 
    }
int solve()
    {
        int st=1 ;
        int dr=n ;
        int gasit=0 ;
        int mijloc ;
        while (st<=dr)
            {
                mijloc=(st+dr)>>1 ;
                if (v[mijloc]==x)
                    {
                        while (v[mijloc]==x)
                              {
                                    mijloc++ ;              
                              }
                        printf ("%d\n%d\n" , mijloc , mijloc) ;
                        while (v[mijloc]==x)
                              {
                                    mijloc-- ;              
                              }
                        printf ("%d" , mijloc) ;
                        return 0;  
                    }  
                else
                    {
                        if (x>v[mijloc])
                            {
                                st=mijloc+1 ;
                            }
                        else
                            {
                                dr=mijloc-1 ;   
                            }
                    }
            } 
        printf ("-1\n") ;
        int i=1 ;
        while (v[i]<x)
            {
                i++ ;    
            }  
        printf("%d\n" , i) ;
        i=n ; 
        while (v[i]>x)
            {
                i-- ;    
            }
        printf("%d\n" , i) ;
        return -1 ;   
    }
     
int main()
{
    freopen (input,"r",stdin) ;
    freopen (output,"w",stdout) ;
     
    read() ;
    solve() ;
  
}