Cod sursa(job #2480554)
Utilizator | Data | 25 octombrie 2019 19:35:40 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.74 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int n,t,x,i,a,v[200005],c,j,ok2,ok1,ok=0,st,dr,mij,b=0;
int main()
{
fin>>n;
for(i=1; i<=n; i++)
fin>>v[i];
fin>>a;
for(i=1; i<=a; i++)
fin>>c>>x;
for(i=1; i<=n; i++)
{
if(c==0)
{
ok=-1;
st=1;
dr=n;
b=0;
while (st<=dr && b==0)
{
mij=(st+dr)/2;
if (v[mij]==x)
{
ok=mij;
b=1;
}
if (v[mij]<=x)
st=mij+1;
else
dr=mij-1;
}
fout<<ok<<endl;
}
if(c==1)
{
ok1=-1;
st=1;
dr=n;
b=0;
while (st<=dr && b==0)
{
mij=(st+dr)/2;
if (v[mij]<=x)
{
st=mij+1;
ok=mij;
b=1;
}
else
dr=mij-1;
}
fout<<ok1<<endl;
}
if(c==2)
{
ok2=-1;
st=1;
dr=n;
b=0;
while (st<=dr && b==0)
{
mij=(st+dr)/2;
if (v[mij]>=x)
{
dr=mij-1;
ok=mij;
b=1;
}
else
st=mij+1;
}
fout<<ok2<<endl;
}
}
return 0;
}