Cod sursa(job #2340772)
Utilizator | Data | 10 februarie 2019 23:10:18 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.87 kb |
#include <fstream>
using namespace std;
ifstream cin ("cautbin.in");
ofstream cout ("cautbin.out");
int v[100002];
int main()
{
int n,m;
cin >> n;
for (int i =1; i<= n; ++ i)
cin >> v[i];
cin >> m;
while (m--)
{
int cerinta,x;
cin >> cerinta >> x;
if (cerinta == 0)
{
int st = 1;
int dr = n;
int sol = 1;
while (st <= dr)
{
int mij = (st + dr) / 2;
if (v[mij] <= x)
{
sol = mij;
st = mij + 1;
}
else
{
dr = mij - 1;
}
}
if (v[sol] == x)
cout << sol << "\n";
else
cout << -1 << "\n";
}
else if (cerinta == 1)
{
int st = 1;
int dr = n;
int sol = 1;
while (st <= dr)
{
int mij = (st + dr) / 2;
if (v[mij] <= x)
{
sol = mij;
st = mij + 1;
}
else
{
dr = mij - 1;
}
}
cout << sol << "\n";
}
else
{
int st = 1;
int dr = n;
int sol = 1;
while (st <= dr)
{
int mij = (st + dr) / 2;
if (v[mij] >= x)
{
sol = mij;
dr = mij - 1;
}
else
{
st = mij + 1;
}
}
cout << sol << "\n";
}
}
return 0;
}