Cod sursa(job #2322812)
Utilizator | Data | 18 ianuarie 2019 13:55:04 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.05 kb |
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,v[100000],i,st,dr,mj,Q,t,x,p;
void caz1(short e)
{
while(st<=dr)
{
mj=(st+dr)/2;
if(x>=v[mj])
{
p=mj;
st=mj+1;
}
else
dr=mj-1;
}
if(e)
g<<p+1<<'\n';
else
{
if(p>-1 && v[p]==x)
g<<p+1<<'\n';
else
g<<-1<<'\n';
}
}
void caz2()
{
while(st<=dr)
{
mj=(st+dr)/2;
if(x<=v[mj])
{
p=mj;
dr=mj-1;
}
else
st=mj+1;
}
g<<p+1<<'\n';
}
int main()
{
f>>n;
for(i=0; i<n; ++i)
f>>v[i];
f>>Q;
do
{
f>>t>>x;
p=-1,st=0,dr=n-1;
switch(t)
{
case 0:
caz1(0);
break;
case 1:
caz1(1);
break;
default :
caz2();
}
}
while(--Q);
}