Cod sursa(job #1001797)
Utilizator | Ilie Ovidiu Horatiu horatiu11 | Data | 26 septembrie 2013 08:58:24 |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.08 kb |
# include <cstdio>
using namespace std;
int n,i,a[100001],m,t,x,mij,st,dr;
int main()
{
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
scanf("%d",&n);
for(i=1;i<=n;++i)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i=1;i<=m;++i)
{
scanf("%d%d",&t,&x);
st=0;dr=n+1;
if(t==0)
{
while(st+1<dr)
{
mij=(st+dr)/2;
if(x<a[mij])dr=mij;
else st=mij;
}
if(a[st]==x)printf("%d\n",st);
else printf("-1\n");
}
else if(t==1)
{
while(st+1<dr)
{
mij=(st+dr)/2;
if(x>=a[mij])st=mij;
else dr=mij;
}
printf("%d\n",st);
}
else
{
while(st+1<dr)
{
mij=(st+dr)/2;
if(x<=a[mij])dr=mij;
else st=mij;
}
printf("%d\n",dr);
}
}
return 0;
}