Cod sursa(job #2170265)

Utilizator dragosmdvMoldovan Dragos dragosmdv Data 14 martie 2018 23:04:40
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <ctime>
#include <vector>

using namespace std;

ifstream fin("cautbin.in");
ofstream fout("cautbin.out");

//vector <int > v;
int v[100002];
int a, n, k, m,x, y ;
int main()
{

    fin>>n;
    for(int i=1; i<=n; i++)
    {
        fin>>v[i];// v.push_back(a);
    }
    fin>>m;
    for(int i=1; i<=m; i++)
    {
        fin>>k>>y;

        if(k==0)
        {
            x=upper_bound(v +1,v+n+1 ,y)-v-1;
            if(v[x]==y)
                fout<<x<<'\n';
            else
                fout<<-1<<'\n';

        }
        else if(k==1)
        {
       for(int i=y; i>=1; i--)
            {
                x=upper_bound(v +1,v+n+1 ,i)-v-1;
                if(v[x]==i)
                {
                    fout<<x<<'\n';
                    break;
                }

            }
        }
        else if(k==2)
        {
            fin>>y;
            for(int i=y; i<=v[n]; i++)
            {
                x=lower_bound(v +1,v+n+1 ,i)-v;
                if(v[x]==i)
                {
                    fout<<x<<'\n';
                    break;
                }

            }


        }

    }

    return 0;
}