Cod sursa(job #1180941)
| Utilizator | Data | 1 mai 2014 14:32:02 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1.13 kb |
#include<fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,a[100005],m;
int main()
{
int i,j,x,y,step;
fin>>n;
for (i=1;i<=n;i++)
fin>>a[i];
fin>>m;
for (j=1;j<=m;j++)
{
fin>>x>>y;
if (x==0)
{
for (step=1;step<=n;step<<=1);
for (i=0;step;step>>=1)
if (i+step<=n && a[i+step]<=y)
i+=step;
if (a[i]==y)
fout<<i<<"\n";
else fout<<"-1\n";
}
if (x==1)
{
for (step=1;step<=n;step<<=1);
for (i=0;step;step>>=1)
if (i+step<=n && a[i+step]<=y)
i+=step;
fout<<i<<"\n";
}
if (x==2)
{
for (step=1;step<=n;step<<=1);
for (i=n;step;step>>=1)
if (i-step>=1 && a[i-step]>=y)
i-=step;
fout<<i<<"\n";
}
}
return 0;
}
