Cod sursa(job #2285322)
Utilizator | Data | 18 noiembrie 2018 14:21:03 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.64 kb |
#include<fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,a[100005];
int main()
{
int i,m,x,y;
fin>>n;
for(i=1; i<=n; i++)
fin>>a[i];
fin>>m;
for(i=1; i<=m; i++)
{
fin>>y>>x;
if(y==0)
{
int st=1,dr=n,rasp=n+1,mij;
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]>x)
{
rasp=mij;
dr=mij-1;
}
else
{
st=mij+1;
}
}
if(a[rasp-1]==x) fout<<rasp-1<<"\n";
else fout<<-1<<"\n";
}
if(y==1)
{
int st=1,dr=n,rasp=n+1,mij;
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]>x)
{
rasp=mij;
dr=mij-1;
}
else
{
st=mij+1;
}
}
fout<<rasp-1<<"\n";
}
if(y==2)
{
int st=1,dr=n,rasp=n+1,mij;
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]>=x)
{
rasp=mij;
dr=mij-1;
}
else
{
st=mij+1;
}
}
fout<<rasp<<"\n";
}
}
}