Cod sursa(job #1350644)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 20 februarie 2015 21:14:23
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
#include <algorithm>
#define Nmax 100099
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");

int n,m,v[Nmax];

inline void ReadInput()
{
    f>>n;
    for(int i=1;i<=n;++i)f>>v[i];
    f>>m;
}

void Querry()
{
    int tip,x;
    f>>tip>>x;
    if(tip==0)
    {
        int poz=upper_bound(v+1,v+n+1,x)-v-1;
        if(poz>=1 && poz<=n && v[poz]==x)g<<poz<<'\n';
        else g<<-1<<'\n';
        return;
    }
    if (tip==1)
    {
        int poz=lower_bound(v+1,v+n+1,x+1)-v-1;
        g<<poz<<'\n';
        return ;
    }
    if(tip==2)
    {
        int poz=upper_bound(v+1,v+n+1,x-1)-v;
        g<<poz<<'\n';
        return ;
    }
}
int main ()
{
    ReadInput();
    // sort(v + 1, v + n + 1); //daca nu sunt sortate
    for(int i=1;i<=m;++i) Querry();
    f.close();g.close();
    return 0;
}