Cod sursa(job #2224850)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 25 iulie 2018 13:11:45
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <iostream>
#include <fstream>

using namespace std;

int num[100005];

int n;

int caut(int x){
    int rez = 0, pas = 1 << 20;
    while(pas){
        if(rez + pas <= n && num[rez + pas] <= x)
            rez += pas;
        pas /= 2;
    }
    return rez;
}

int main()
{
    ifstream fin("cautbin.in");
    ofstream fout("cautbin.out");
    fin >> n;
    for(int i = 1; i <= n; ++i)
        fin >> num[i];
    int m, op, x;
    fin >> m;
    for(int q = 1; q <= m; ++q){
        fin >> op >> x;
        int pos = caut(x);
        if(op == 2)
            pos = caut(x - 1) + 1;
        fout << pos << "\n";
    }
    return 0;
}