Cod sursa(job #1898880)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 2 martie 2017 12:38:54
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stdio.h>
#define NMAX 100005
using namespace std;
ifstream f ("cautbin.in");
ofstream g ("cautbin.out");
int v[NMAX], n, m;
int main()
{
    int t, y, x;
    f>>n;
    for (int i = 1; i <= n; i++)
        f>>v[i];
    f>>m;
    for (int i = 1; i <= m; i++)
    {
        f>>t>>y;
        if (t == 0)
        {
            x = upper_bound(v + 1, v + 1 + n, y) - v - 1;
            if (x <= n && x >= 1 && v[x] == y)
                g<<x<<'\n';
            else g <<"-1\n";
        }
        else if (t == 1)
        {
            x = lower_bound(v + 1, v + 1 + n, y + 1) - v - 1;
            g<<x<<'\n';
        }
        else
        {
            x = upper_bound(v + 1, v + 1 + n, y - 1) - v;
            g<<x<<'\n';
        }
    }
    return 0;
}