Cod sursa(job #2290169)

Utilizator Dusceac_Bogdan24Dusceac Bogdan Dusceac_Bogdan24 Data 25 noiembrie 2018 21:29:49
Problema Cautare binara Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 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){
            if(v[pozx]==x){
                j=0;
                while(x==v[pozx-j]){
                    r=pozx-j;
                        j++;
                    }
                    fout<<r<<'\n';
            }
            else{
                fout<<pozx+1<<'\n';
            }

        }
    }

    return 0;
}