Pagini recente » Cod sursa (job #2858549) | Cod sursa (job #3285664) | Cod sursa (job #1837665) | Cod sursa (job #43692) | Cod sursa (job #665005)
Cod sursa(job #665005)
#include <cstdio>
using namespace std;
const int MAXN = 100010;
short tip_test;
int x,n;
int v[MAXN];
int cautare_binara (int nr)
{
int poz = 0;
for (int pas = 1 << 18;pas >= 1;pas >>= 1)
if (poz + pas < n && v[poz + pas] < nr)
poz += pas;
return poz;
}
void interpretare_test()
{
int poz;
if (tip_test == 0)
{
poz = cautare_binara(x + 1);
if (v[poz] == x)
printf ("%d",poz);
else printf ("-1");
}
if (tip_test == 1)
{
poz = cautare_binara(x + 1);
if (v[poz] <= x)
printf ("%d",poz);
else printf ("-1");
}
if (tip_test == 2)
{
poz = cautare_binara(x);
if (v[poz + 1] >= x)
printf ("%d",poz + 1);
else printf ("-1");
}
printf ("\n");
}
void citire()
{
int nr_teste;
freopen ("cautbin.in","r",stdin);
freopen ("cautbin.out","w",stdout);
scanf("%d",&n);
for (int i = 1;i <= n;++i)
scanf ("%d",&v[i]);
scanf ("%d",&nr_teste);
for (int i = 1;i <= nr_teste;++i)
{
scanf ("%hd %d",&tip_test,&x);
interpretare_test();
}
}
int main()
{
citire();
return 0;
}