Cod sursa(job #2137881)
| Utilizator | Data | 21 februarie 2018 08:48:55 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,m,i,p,x,v[100001],y;
int main()
{
fin>>n;
for (i=1;i<=n;i++)
fin>>v[i];
fin>>m;
for (i=1;i<=m;i++)
{
fin>>p>>x;
if (p==0)
{
y=upper_bound(v+1,v+n+1,x)-v;
if (v[y-1]==x) fout<<y-1<<'\n';
else fout<<"-1"<<'\n';
}
if (p==1)
{
y=upper_bound(v+1,v+n+1,x)-v;
fout<<y-1<<'\n';
}
if (p==2)
{
y=lower_bound(v+1,v+n+1,x)-v;
fout<<y<<'\n';
}
}
return 0;
}
