Cod sursa(job #2705351)

Utilizator ZamfiroiuBogdanZamfiroiu Bogdan Marian ZamfiroiuBogdan Data 12 februarie 2021 14:07:56
Problema Cautare binara Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <iostream>
#include <fstream>

using namespace std;

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

long long A[500001], i, ok, n, x, st, dr, mij, z;

int main()
{
    f >> n >> x;

    for (i = 1; i <= n; i++) f >> A[i];

    f >> z;

    st = 1;
    dr = n;

    if (z == 0)
    {
        while (st <= dr)
        {
            mij = (st + dr) / 2;
            if (x >= A[mij]) st = mij + 1;
                else dr = mij - 1;
        }

        if (A[st - 1] == x) cout << st - 1 << '\n';
            else cout << -1 << '\n';
    }

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

        cout << st - 1 << '\n';
    }

    if (z == 2)
    {
        while (st <= dr)
        {
            mij = (st + dr) / 2;
            if (x <= A[mij]) dr = mij - 1;
                else st = mij + 1;
        }
        cout << dr + 1 << '\n';
    }

    return 0;
}