Cod sursa(job #2930527)
Utilizator | Data | 28 octombrie 2022 16:50:37 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.56 kb |
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,m,i,v[100001],t,poz,mij,x;
fin>>n;
for(i=1; i<=n; i++)
fin>>v[i];
fin>>m;
for(i=1; i<=m; i++)
{
fin>>t>>x;
if(t==0)
{
int dr=n, st=1;
poz=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]==x)
{
poz=mij;
st=mij+1;
}
else if(v[mij]<x)
st=mij+1;
else
dr=mij-1;
}
fout<<poz<<"\n";
}
else if(t==1)
{
int st=1,dr=n;
poz=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]<=x)
{
st=mij+1;
poz=mij;
}
else
dr=mij-1;
}
fout<<poz<<"\n";
}
else
{
int st=1,dr=n;
poz=-1;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>=x)
{
dr=mij-1;
poz=mij;
}
else
st=mij+1;
}
fout<<poz<<"\n";
}
}
return 0;
}