Cod sursa(job #2384432)
Utilizator | Virtosu Alexandra Mihaela VirtosuAlexandra | Data | 20 martie 2019 18:53:39 |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.4 kb |
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int n, m, v[100000], x, cod;
int main()
{
fin>>n;
for(int i=1; i<=n; i++)fin>>v[i];
fin>>m;
for(int i=1; i<=m; i++)
{
fin>>cod>>x;
if(cod==0)
{
int st=1, dr=n, mij=0;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]<=x)
st=mij+1;
else dr=mij-1;
}
if(v[dr]==x)fout<<dr<<"\n";
else fout<<"-1\n";
}
if(cod==1)
{
int st=1, dr=n, mij=0, poz=0;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]<=x)
{
st=mij+1;
poz=mij;
}
else if(v[mij]>x)
{
dr=mij-1;
}
}
fout<<poz<<"\n";
}
if(cod==2)
{
int st=1, dr=n, mij=0, poz=0;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>=x)
{
poz=mij;
dr=mij-1;
}
else st=mij+1;
}
fout<<poz<<"\n";
}
}
return 0;
}