Cod sursa(job #3360526)

Utilizator PetruVPetru Varabiescu PetruV Data 14 iulie 2026 15:01:24
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");

const int MAXN = 100000;

int n;
int v[MAXN];

int c0(int x) {
    int st, dr, mij, ans;

    ans = -1;
    st = 0;
    dr = n - 1;

    while (st <= dr) {
        mij = st + (dr - st) / 2;

        if (v[mij] <= x) {
            st = mij + 1;
            ans = mij + 1;
        }
        else
            dr = mij - 1;
    }
    return ans;
}

int c1(int x) {
    int st, dr, mij, ans;

    st = 0;
    dr = n - 1;
    ans = -1;
    while (st <= dr) {
        mij = st + (dr - st) / 2;
        if (v[mij] <= x) {
            st = mij + 1;
            ans = mij + 1;
        }
        else
            dr = mij - 1;
    }
    return ans;
}

int c2(int x) {
    int st, dr, ans, mij;

    st = 0;
    dr = n - 1;
    ans = -1;

    while (st <= dr) {
        mij = st + (dr - st) / 2;
        if (v[mij] >= x) {
            ans = mij + 1;
            dr = mij - 1;
        } else
            st = mij + 1;
    }
    return ans;
}

int main() {

    int m;

    fin >> n;
    for (int i = 0; i < n; i++)
        fin >> v[i];

    fin >> m;
    for (int i = 0; i < m; i++) {
        int c, x;
        fin >> c >> x;

        if (c == 0)
            fout << c0 (x) << '\n';
        else if (c == 1)
            fout << c1 (x) << '\n';
        else if (c == 2)
            fout << c2 (x) << '\n';
    }

    return 0;
}