Cod sursa(job #864954)

Utilizator gabrielvGabriel Vanca gabrielv Data 25 ianuarie 2013 21:29:33
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#include <algorithm>

#define NMAX 100015

using namespace std;

int N,M,X,v[NMAX],op,poz;

void Read()
{
    freopen("cautbin.in","r",stdin);
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
        scanf("%d",&v[i]);
    scanf("%d",&M);
}

int main()
{
    Read();
    freopen("cautbin.out","w",stdout);
    sort(V + 1, V + N + 1);
    while(M--)
    {
        scanf("%d%d",&op,&X);
        if(op==0)
        {
            poz=upper_bound(v+1,v+N+1,X)-v-1;
            if(poz<=N && poz>=1 && v[poz] == X)
                printf("%d\n",poz);
            else
                printf("-1");
        }
        else
            if(op==1)
            {
                poz=lower_bound(v+1,v+N+1,X+1)-v-1;
                printf("%d\n",poz);
            }
            else
            {
                poz=upper_bound(v+1,v+N+1,X-1)-v;
                printf("%d\n",poz);
            }
    }
    return 0;
}