Cod sursa(job #2064005)

Utilizator cyg_ionutStan Ionut Gabriel cyg_ionut Data 11 noiembrie 2017 18:04:25
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <cstdio>
using namespace std;

int v[100005], n, m, pos;

int funct_0(int x) {
    pos = 0;
    for (int bit = 1 << 17; bit > 0; bit /= 2)
        if (bit + pos <= n && v[bit + pos] <= x)
            pos = pos + bit;
    if (v[pos] == x)
        return pos;
    else
        return -1; }

int funct_1(int x) {
    pos = 0;
    for (int bit = 1 << 17; bit > 0; bit /= 2)
        if (bit + pos <= n && v[bit + pos] <= x)
            pos = pos + bit;
    return pos; }

int funct_2(int x) {
    pos = 0;
    for (int bit = 1 << 17; bit > 0; bit /= 2)
        if (bit + pos <= n && v[bit + pos] < x)
            pos = pos + bit;
    return pos + 1; }

int main() {
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);
    int c, x;

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &v[i]);

    scanf("%d", &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d %d", &c, &x);
        if (c == 0)
            printf("%d\n", funct_0(x));
        if (c == 1)
            printf("%d\n", funct_1(x));
        if (c == 2)
            printf("%d\n", funct_2(x)); }

    return 0; }