Cod sursa(job #694543)

Utilizator attila3453Geiszt Attila attila3453 Data 27 februarie 2012 21:40:39
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;

int *v;

int main () {
    int n, m, x, i, t, k;

    freopen("cautbin.in", "r", stdin);
    freopen("cautbin.out", "w", stdout);

    scanf("%d", &n);

    v = new int[n+1];

    for (i = 1; i <= n; ++ i)
		scanf("%d", &v[i]);

	scanf("%d", &m);

	while(--m)
	{
		scanf("%d %d", &t, &k);

		switch(t)
		{
			case 0:
				x = upper_bound(v + 1, v + n + 1, k) - v - 1;

				if(x <= n && x > 0 && v[x] == k)
					printf("%d\n", x);
				else
					printf("-1\n");
				continue;
			case 1:
				x = lower_bound(v + 1, v + n + 1, k + 1) - v - 1;
				printf("%d\n", x);
			case 2:
				x = upper_bound(v + 1, v + n + 1, k - 1) - v;
				printf("%d\n", x);
		}
	}
}