Cod sursa(job #1116775)

Utilizator gapdanPopescu George gapdan Data 22 februarie 2014 20:10:46
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
int v[100001];
int x,i,n;
int main()
{
    freopen("cautbin.in","r",stdin);
    freopen("cautbin.out","w",stdout);
    scanf("%d",&n);
    for (i=1;i<=n;++i)
        {
            scanf("%d",&x);
            v[i]=x;
        }
    int  k,cst,x;
    scanf("%d",&k);
    for (i=1;i<=k;++i)
    {
        scanf("%d%d",&cst,&x);
        if (cst==0)
        {
            int *p=upper_bound(v+1,v+n+1,x);
            if (v[p-v-1]==x) printf("%d\n",p-v-1);
                else printf("-1\n");
        }
        else if (cst==1)
        {
            int *p=upper_bound(v+1,v+n+1,x);
            if (v[n]<=x) printf("%d\n",n);
                else printf("%d\n",p-v-1);
        }
        else
        {
            int *p=lower_bound(v+1,v+n+1,x);
           if (v[1]>=x) printf("1\n");
                else printf("%d\n",p-v);

        }
    }
    return 0;
}