Cod sursa(job #2643129)

Utilizator ggutaGuta George gguta Data 18 august 2020 19:54:26
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("in.txt");
ofstream g("out.txt");

int v[100001], n, i, mij, x, quest, m, st, dr;

void cautare() {
    st = 1;
    dr = n;
    while (p <= q) {
        mij = (s + d) / 2;
        if (a[mij] == x)
            break;
        else if (a[mij] > x)
            d = mij - 1;
        else if (a[mij] < x)
            s = mij + 1;
    }
}


int main() {
    f >> n;
    for (i = 1; i <= n; i++)
        f >> a[i];
    f >> m;
    for (i = 1; i <= m; i++) {
        f >> quest >> x;
        if (quest == 0) {
            cautare(x);
            if (a[mij] == x) {
                while (a[mij + 1] == x)
                    mij++;
                g << mij << '\n';
            } else
                g << -1 << '\n';
        }

        if (quest == 1) {
            cautare(x);
            if (a[mij] == x) {
                while (a[mij + 1] == x)
                    mij++;
            } else if (a[mij] > x)
                mij--;
            g << mij << '\n';
        }
        if (quest == 2) {
            cautare(x);
            if (a[mij] == x) {
                while (a[mij - 1] == a[mij])
                    mij--;
            } else if (a[mij] < x)
                mij++;
            g << mij << '\n';
        }
    }

    g.close();
    f.close();
    return 0;
}