Cod sursa(job #818632)

Utilizator unholyfrozenCostea Andrei unholyfrozen Data 17 noiembrie 2012 19:21:05
Problema Cautare binara Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb

int main()
{
	freopen("cautbin.in", "r", stdin);
	freopen("cautbin.out", "w", stdout);
	
	{
		int num_elem, *Array, num_tests;

		scanf ("%d", &num_elem);

		Array = new int[N];
		for (int i = 0; i < num_elem; i++)
			scanf ("%d", &Array[i]);
		
		scanf ("%d", &num_tests);
		for (int j = 0; j < num_tests; j++) {
			int type, elem;

			scanf ("%d %d", &type, &elem);
			int i, binary_step;
				
			for (binary_step = 1; binary_step <= num_elem; binary_step <<=1);
			
			for (i = -1; binary_step; binary_step >>= 1)
				if (i + binary_step < num_elem &&
				   (type < 2 && V[i + binary_step] <= elem ||
				    type == 2 && V[i + binary_step] < elem))
					i += binary_step;

			int res;
			switch (type) {
				case 0:
					res = i == -1 || V[i] != elem ? -1 : i;
					break;
				case 1:
				case 2:
					res = i; 
					break;
			}

			printf ("%d\n", res);

		}
	}		
	return 0;
}