Cod sursa(job #2782774)

Utilizator StefanL2005Stefan Leustean StefanL2005 Data 12 octombrie 2021 23:32:09
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream in ("cautbin.in") ;
ofstream out ("cautbin.out") ;

int main()
{
    int n , m ;
    in>> n ;
    vector<long long int> v(n + 1 , 0) ;
    for (int i = 1 ; i <= n ; i ++){
        in>> v[i] ;
    }
    in>> m ;
    for (int i = 1 ; i <= m ; i ++){
        int nrc , nr , c1 = 1 , f1 = n  , mij1 , c2 = 1 , f2 = n , mij2 ;
        in>> nrc >> nr ;
        while (f1 - c1 <= 1 && f2 - c2) {
            mij1 = (c1 + f1) / 2 ;
            mij2 = (c2 + f2) / 2 ;
            if (nr <= v[mij1]) {
                f1 = mij1 ;
            }
            else {
                c1 = mij1 ;
            }
            if (nr >= v[mij2]) {
                c2 = mij2 ;
            }
            else {
                f2 = mij2 ;
            }
        }
        if (v[c1] != nr) {
            c1 = f1 ;
        }
        if (v[f2] != nr) {
            f2 = c2 ;
        }
        if (nrc == 0) {
            if (v[f2] == nr) {
                out<< f2 <<endl ;
            }
            else {
                out<< -1 <<endl ;
            }
        }
        if (nrc == 1) {
            out<< f2 <<endl ;
        }
        if (nrc == 2) {
            out<< c1 <<endl ;
        }

    }
    return 0;
}