Cod sursa(job #1916170)

Utilizator stefan_gheorgheGheorghe Stefan stefan_gheorghe Data 9 martie 2017 07:47:31
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int a[nmax],n,m;
void load()
{
    f>>n;
    for(int i=1; i<=n; i++)
        f>>a[i];
}
inline int C1(int x)
{
    int st=1,dr=n,poz=-1,mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(a[mij]==x)
            poz=mij,st=mij+1;
        else if(a[mij]<x)st=mij+1;
        else dr=mij-1;
    }
    return poz;
}
inline int C2(int x)
{
    int st=1,dr=n,poz=-1,mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(a[mij]<=x)
            poz=mij,st=mij+1;
        else dr=mij-1;
    }
    return poz;
}
inline int C3(int x)
{
    int st=1,dr=n,poz=-1,mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(a[mij]>=x)
            poz=mij,dr=mij-1;
        else st=mij+1;
    }
    return poz;
}
int main()
{
    load();
    int op,x;
    f>>m;
    for(int i=1; i<=m; i++)
    {
        f>>op>>x;
        if(op==0) g<<C1(x)<<"\n";
        else if(op==1) g<<C2(x)<<"\n";
                else g<<C3(x)<<"\n";
    }
    return 0;
}