Cod sursa(job #1869702)
Utilizator | Data | 6 februarie 2017 08:43:55 | |
---|---|---|---|
Problema | Cautare binara | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.67 kb |
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n,x,a[100005],i,j,m,y,z;
int main()
{
f>>n;
for(i=1;i<=n;i++) f>>a[i];
f>>m;
while(m)
{
f>>x>>y;
if(x==0)
{
z=upper_bound(a+1,a+n+1,y)-a;
if(a[z-1]==y) g<<z-1;
else g<<"-1";
g<<'\n';
}
else if(x==1)
{
z=upper_bound(a+1,a+n+1,y)-a;
g<<z-1<<'\n';
}
else
{
z=lower_bound(a+1,a+n+1,y)-a;
g<<z<<'\n';
}
m--;
}
return 0;
}