Cod sursa(job #2427829)
Utilizator | Data | 2 iunie 2019 13:36:14 | |
---|---|---|---|
Problema | Cautare binara | Scor | 0 |
Compilator | c-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.7 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("cautbin.in");
ofstream out ("cautbin.out");
int v[100001];
int main()
{
int n, m, x, st, dr, mij, i, p, j, pos;
in >> n;
for (i=1; i<=n; i++)
in >> v[i];
in >> m;
for (j=1; j<=m; j++)
{
in >> p >> x;
if(p==0)
{
st=1;
dr=n;
pos=-1;
while (st<dr)
{
mij=(st+dr)/2;
if (v[mij]<=x)
{
st=mij+1;
pos=mij;
}
else
dr=mij-1;
}
if(v[pos]==x)
out << pos;
else
out << "-1";
out << endl;
}
if (p==1)
{
st=1;
dr=n;
pos=-1;
while (st<=dr)
{
mij=(st+dr)/2;
if (v[mij]<=x)
{
st=mij+1;
pos=mij;
}
else
dr=mij-1;
}
if (v[pos]<=x)
out << pos;
out << endl;
}
if (p==2)
{
st=1;
dr=n;
pos=+1;
while (st<=dr)
{
mij=(st+dr)/2;
if (v[mij]>=x)
{
dr=mij-1;
pos=mij;
}
else
st=mij+1;
}
if (v[pos]>=x)
out << pos;
out << endl;
}
}
return 0;
}