Cod sursa(job #3157697)

Utilizator Alexbora13Bora Ioan Alexandru Alexbora13 Data 16 octombrie 2023 17:05:16
Problema Cautare binara Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
#define MAX 100000
using namespace std;

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

int v[MAX+1];
int m, n, c, x;
/*
lower_bound = cel mai din stanga element care este mai mare sau egal cu o valoare cautata
upper_bound = cel mai din stanga element care este mai mare decat o valoare cautata
*/
int main()
{
    fin >> n;
    for(int i=0; i<n; i++)fin >> v[i];
    fin >> m;
    for(int i=0; i<m; i++)
    {
        fin >> c >> x;
        if(c==0)
        {
            int pos = upper_bound(v,v+n,x)-v-1;
            if(v[pos]==x)fout << pos+1 << '\n';
            else fout << -1 << '\n';
        }
        else if(c==1)
        {
            int pos = upper_bound(v,v+n,x)-v-1;
            fout << pos+1 << '\n';
        }
        else if(c==2)
        {
            int pos=lower_bound(v,v+n,x)-v;
            fout << 1+pos << '\n';
        }
    }
    return 0;
}