Cod sursa(job #2417714)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 30 aprilie 2019 21:27:47
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int c,v[100005],n,m,x,t,st,dr,mij;
int main()
{
    f >> n;
    for (int i=1;i<=n;i++) {
        f >> v[i];
    }
    f >> m;
    for (int k=1;k<=m;k++) {
        f >> c >> x;
        st=1;
        dr=n;
        if (c==0) {
            mij=(st+dr+1)/2;
            while (st!=dr) {
                if (v[mij]<=x) {
                    st=mij;
                }
                else {
                    dr=mij-1;
                }
                mij=(st+dr+1)/2;
            }
            if (v[st]==x)
                g << mij<<'\n';
            else
                g << -1 << '\n';
        }
        else if (c==1)  {
            mij=(st+dr+1)/2;
            while (st!=dr) {
                if (v[mij]<=x) {
                    st=mij;
                }
                else {
                    dr=mij-1;
                }
                mij=(st+dr+1)/2;
            }
                g << mij<<'\n';
        }
        else {
             while (st!=dr)
            {
                mij=(st+dr)/2;
                if (v[mij]>=x)
                {
                    dr=mij;
                }
                else
                    st=mij+1;
            }
            g<<st<<'\n';
        }
    }
    return 0;
}