Mai intai trebuie sa te autentifici.
Cod sursa(job #1498000)
| Utilizator | Data | 7 octombrie 2015 21:15:57 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1.01 kb |
#include <cstdio>
using namespace std;
int v[100010];
int logn,n;
int cauta_binar(int s)
{
int poz=0;
for(int i=logn;i>=0;i--)
if(poz+(1<<i)<=n && v[poz+(1<<i)]<=s) poz+=1<<i;
return poz;
}
int cauta_binar2(int s)
{
int poz=0;
for(int i=logn;i>=0;i--)
if(poz+(1<<i)<=n && v[poz+(1<<i)]<s) poz+=1<<i;
return poz+1;
}
int main()
{
freopen("cautbin.in","r",stdin);
freopen("cautbin.out","w",stdout);
int m,x,y,poz,a;
scanf("%d",&n);
for(logn=1;(1<<logn)<=n;logn++);
logn--;
for(int i=1;i<=n;i++)
scanf("%d",&v[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x==0) {poz=cauta_binar(y);
if(v[poz]==y) printf("%d\n",poz);
else printf("-1\n");}
if(x==1) {poz=cauta_binar(y);
printf("%d\n",poz);}
if(x==2) {poz=cauta_binar2(y);
printf("%d\n",poz);}
}
return 0;
}
