Cod sursa(job #501733)

Utilizator skullLepadat Mihai-Alexandru skull Data 16 noiembrie 2010 12:38:28
Problema Cautare binara Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <stdio.h>
#include <algorithm>
using namespace std;
#define nmax 100005

int v[nmax];

int main ()
{
	int tcount, t, x, y, k, n, i;
	freopen("cautbin.in","r",stdin);
	freopen("cautbin.out","w",stdout);
	scanf("%d ", &n);
	for (i = 1; i <= n; ++i) scanf("%d ", &v[i]);
	scanf("%d ", &t);
	for (tcount = 1; tcount <= t; ++tcount)
	{
		scanf("%d %d ", &k, &x);
		if ( !k )
		{
			y = upper_bound(v + 1, v + n + 1, x) - v - 1;
			if (y>=1 && y<=n && v[y] == x)
				printf("%d\n",y);
			else
				printf("-1\n");
		}
		if (k == 1)
		{
			y = lower_bound(v + 1, v + n + 1, x + 1) - v - 1; printf("%d\n", y);
		}
		if (k == 2)
		{
			y = upper_bound(v + 1, v + n + 1, x - 1) - v; printf("%d\n", y);
		}
	}
	return 0;
}