Cod sursa(job #3204837)

Utilizator razviii237Uzum Razvan razviii237 Data 17 februarie 2024 18:38:26
Problema Cautare binara Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int v[10001],f[1001];
int main()
{
    int n, i,m,j,x,OK=1,k=0,d,q=0,mij,t;
    fin>>n;
    for(i=1; i<=n; i++)
    {
        fin>>v[i];
    }
    fin>>m;
    for(i=1; i<=m; i++)
    {
        fin>>t;
        fin>>x;

        if(t == 2) {
            int st=1, dr=n;
            while(st!=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>=x)
                {
                    dr=mij;
                }
                else st=mij+1;
            }
            fout<<st << endl;
        } else if(t == 0) {
            x ++;
            int st=1, dr=n;
            while(st!=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>=x)
                {
                    dr=mij;
                }
                else st=mij+1;
            }
            st --;
            x --;
            if(v[st] == x)
                fout << st << endl;
            else
                fout << -1 << endl;

        } else if(t == 1) {
            x ++;
            int st=1, dr=n;
            while(st!=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]>=x)
                {
                    dr=mij;
                }
                else st=mij+1;
            }
            st --;
            fout << st << endl;
        }
    }
    return 0;
}