Cod sursa(job #1313287)

Utilizator Burbon13Burbon13 Burbon13 Data 10 ianuarie 2015 15:19:35
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <cstdio>
#include <algorithm>

#define n_max 100005

using namespace std;

int n , v[n_max] ;

void citire() ;
void parcurgere() ;

int main()
{
    freopen( "cautbin.in" , "r" , stdin ) ;
    freopen( "cautbin.out" , "w" , stdout ) ;
    citire() ;
    parcurgere() ;
    return 0;
}

void citire()
{
    scanf( "%d" , &n ) ;
    for ( int i = 0 ; i < n ; i ++ )
        scanf( "%d" , &v[i] ) ;
    sort( v , v + n ) ;
}

void parcurgere()
{
    int k ;
    scanf( "%d" , &k ) ;
    while(k--)
    {
        int t , nr ;
        scanf( "%d %d" , &t , &nr ) ;
        if ( t == 0 )
        {
            int x = upper_bound( v , v + n , nr ) - v - 1 ;
            if ( v[x] == nr )
                printf( "%d\n" , x + 1 ) ;
            else
                printf( "-1\n" ) ;
        }
        else if ( t == 1 )
        {
            int x = lower_bound( v , v + n , nr + 1 ) - v - 1  ;
            printf( "%d\n" , x + 1 ) ;
        }
        else
        {
            int x = upper_bound( v , v + n , nr - 1 ) - v ;
            printf( "%d\n" , x + 1 ) ;
        }
    }
}