Pagini recente » probleme | Cod sursa (job #150879) | Cod sursa (job #1556536) | Cod sursa (job #1367928) | Cod sursa (job #434730)
Cod sursa(job #434730)
#include <stdio.h>
#include <stdlib.h>
int n,nr,vec[110000],tip,num;
int comparator(const void* a1,const void* a2) {
int _a1 = (*(int*)a1);
int _a2 = (*(int*)a2);
if(_a1 > _a2) return 1;
if(_a1 ==_a2) return 0;
return -1;
}
int main() {
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%d",&n);
for(int i = 0 ; i< n; i++)
scanf("%d",&vec[i]);
scanf("%d",&nr);
for(int i = 1 ; i<=nr; nr++) {
scanf("%d %d",&tip,&num);
int*addr = (int*)bsearch( (void*)(&num),(void*)vec,n,sizeof(vec[0]),comparator);
if(tip == 0) {
int poz = -1;
if(addr == NULL)
printf("-1\n");
else {
poz = addr - vec;
while( (poz < n-1) && (vec[poz] == vec[poz+1]) )
poz++;
printf("%d",poz+1);
}
}
if(tip == 2) {
int poz = addr - vec;
while(vec[poz] == vec[poz-1])
poz--;
poz--;
printf("%d",poz+1);
}
if(tip == 1) {
int poz = addr - vec;
while(vec[poz] == vec[poz+1])
poz++;
poz++;
printf("%d",poz+1);
}
}
return 0;
}