Cod sursa(job #2072474)
Utilizator | Data | 21 noiembrie 2017 21:28:54 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.47 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,m,i,t[100001], s,d,mij,x,tip;
int main()
{
cin>>n;
for (i=1;i<=n;i++)
cin>>t[i];
cin>>m;
for (i=1;i<=m;i++)
{
cin>>tip>>x;
if (tip==0)
{
s=1;
d=n;
int poz=-1;
while (s<=d)
{
mij=(s+d)/2;
if (x==t[mij])
poz=mij;
if (x>=t[mij])
s=mij+1;
else d=mij-1;
}
cout<<poz<<endl;
}
else if (tip==1)
{
s=1;
d=n;
int poz=-1;
while (s<=d)
{
mij=(s+d)/2;
if (x>=t[mij])
{
poz=mij;
s=mij+1;
}
else d=mij-1;
}
cout<<poz<<endl;
}
else
{
s=1;
d=n;
int poz=-1;
while (s<=d)
{
mij=(s+d)/2;
if (x<=t[mij])
{
poz=mij;
d=mij-1;
}
else s=mij+1;
}
cout<<poz<<endl;
}
}
return 0;
}