Cod sursa(job #1182782)

Utilizator bogdanmarin69Bogdan Marin bogdanmarin69 Data 7 mai 2014 17:13:28
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include <cstdio>
using namespace std;
#define MAX 100001
int v[MAX],n,x,m;
int main()
{
    int i,st,dr,mj,cod,x,poz1,poz,poz2;
    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out","w",stdout);
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",v+i);
    scanf("%d",&m);
    for(i=1;i<=m;i++){
        scanf("%d%d",&cod,&x);
        if(cod==0 or cod==1){
            st=1;dr=n;poz=-1;
            while(st<=dr){
                mj=st+(dr-st)/2;
                if(x<v[mj])
                    dr=mj-1;
                else{//v[mj] este mai mic sau egal ca x
                    if(x==v[mj])
                        poz=mj;
                    poz1=mj;
                    st=mj+1;
                }
            }
            if(cod==0) printf("%d\n",poz);
            if(cod==1) printf("%d\n",poz1);
        }
        else{
            st=1;dr=n;
            while(st<=dr){
                mj=st+(dr-st)/2;
                if(x<=v[mj]){
                    poz2=mj;
                    dr=mj-1;
                }

                else
                    st=mj+1;
            }
            printf("%d\n",poz2);
        }
    }

    return 0;
}