Cod sursa(job #2334513)
| Utilizator | Data | 2 februarie 2019 18:06:12 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 40 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.19 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream in("cautbin.in");
ofstream out("cautbin.out");
int n, nr[1001], m, tI, x, i, j, poz;
int main()
{
in >> n;
for(i = 1; i <= n; i++)
in >> nr[i];
in >> m;
for(i = 1; i <= m; i++)
{
in >> tI >> x;
poz = 0;
if(tI == 0)
{
for(j = 1 << 20; j > 0; j/=2)
{
if(poz + j <= n
&& nr[poz + j] <= x)
poz += j;
}
if(nr[poz] == x)
out << poz << '\n';
else
out << -1 << '\n';
}
if(tI == 1)
{
for(j = 1 << 20; j > 0; j/=2)
{
if(poz + j <= n
&& nr[poz + j] <= x)
poz += j;
}
if(nr[poz] <= x)
out << poz << '\n';
}
if(tI == 2)
{
for(j = 1; j <= n; j++)
if(nr[j] >= x)
{
out << j << '\n';
break;
}
}
}
return 0;
}
