Cod sursa(job #2290191)

Utilizator Dusceac_Bogdan24Dusceac Bogdan Dusceac_Bogdan24 Data 25 noiembrie 2018 22:24:00
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int main()
{
    int N,M,mid,st,dr,i,intr,pozx,j,r,v[100005];
    fin>>N;
    unsigned int x;
    st=1;
    dr=N;
    for(i=1;i<=N;i++){
        fin>>v[i];
    }
    fin>>M;
    for(i=1;i<=M;i++){
        fin>>intr>>x;
        st=1;
        dr=N;
        while(st<dr){
            mid=st+(dr-st+1)/2;
            if(v[mid]>x)
                dr=mid-1;
            else
                st=mid;
        }
        pozx=dr;
        if(intr!=2){
            if(v[pozx]==x){
                fout<<pozx<<'\n';
            }
            else{
                if(intr==0){
                    fout<<-1<<'\n';
                }
                if(intr==1){
                    fout<<pozx<<'\n';
                }
            }
        }
        if(intr==2){
            st=1;
            dr=N;pozx=N+1;
            while(st<=dr){
                mid=(st+dr)/2;
                if(v[mid]>=x){
                    pozx=mid;
                    dr=mid-1;
                }
                else{
                    st=mid+1;
                }
            }
            fout<<pozx<<"\n";
        }
    }
    return 0;
}