Pagini recente » Cod sursa (job #1761108) | Cod sursa (job #2590377) | Cod sursa (job #326170) | Cod sursa (job #2070694) | Cod sursa (job #525861)
Cod sursa(job #525861)
#include <cstdio>
using namespace std;
int *a, n;
int cautBin(int val, int type) {
int step;
for (step = 1; step < n; step<<=1);
int i;
for (i = 0; step; step>>=1){
if (i + step < n && a[i + step] <= val){
i +=step;
}
}
if ((a[i] != val)&&(type == 0)) return -1;
else if (type == 2) {
while (a[i] >= val) i--;
return i+2;
}
return i+1;
}
int main() {
freopen("cautbin.in","r", stdin);
freopen("cautbin.out","w", stdout);
int t;
scanf("%d\n", &n);
a = new int[n];
int i;
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
scanf("%d", &t);
int type, nr;
for (i = 0; i < t; i++) {
scanf("%d %d", &type, &nr);
printf("%d\n", cautBin(nr, type));
}
delete []a;
return 0;
}