Cod sursa(job #2278203)

Utilizator maria_neagoieMaria Neagoie maria_neagoie Data 7 noiembrie 2018 14:22:35
Problema Cautare binara Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <cstdio>
#include <algorithm>
using namespace std;
const int L=16;
int v[100005];
int c0(int x,int n)
{
    int r=0,pas=1<<L;
    while(pas)
    {
        if(v[r+pas]<=x && r+pas<=n)
            r=r+pas;
        pas=pas/2;
    }
    if(v[r]!=x)
        return -1;
    return r;
}
int c1(int x,int n)
{
    int r=0,pas=1<<L;
    while(pas)
    {
        if(v[r+pas]<=x && r+pas<=n)
            r=r+pas;
        pas=pas/2;
    }
    return r;
}
int c2(int x,int n)
{
    int r=0,pas=1<<L;
    while(pas)
    {
        if(v[r+pas]<x && r+pas<=n)
            r=r+pas;
        pas=pas/2;
    }
    return r+1;
}
int main()
{
    freopen("cautbin.in","r",stdin);
    freopen("cautbin.out","w",stdout);
    int n,i,m,t,x;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&v[i]);
    sort(v+1,v+n+1);
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&t,&x);
        if(t==0)
            printf("%d\n",c0(x,n));
        if(t==1)
            printf("%d\n",c1(x,n));
        if(t==2)
            printf("%d\n",c2(x,n));
    }
    return 0;
}