Cod sursa(job #2289327)
Utilizator | Data | 24 noiembrie 2018 13:17:04 | |
---|---|---|---|
Problema | Cautare binara | Scor | 20 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.45 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int main()
{
int N,M,mid,st,dr,i,x,intr,pozx,j,r,v[100005];
fin>>N;
st=1;
dr=N;
for(i=1;i<=N;++i){
fin>>v[i];
}
fin>>M;
for(i=1;i<=M;++i){
fin>>intr>>x;
st=1;
dr=N;
while(st<dr){
mid=st+(dr-st+1)/2;
if(v[mid]>x)
dr=mid-1;
else
st=mid;
}
pozx=dr;
if(intr!=2){
if(v[pozx]==x){
fout<<pozx<<'\n';
}
else{
if(intr==0){
fout<<-1<<'\n';
}
if(intr==1){
j=1;
while(x>v[j]){
r=j;
j++;
}
fout<<r<<'\n';
}
}
}
if(intr==2){
if(v[pozx]==x){
j=0;
while(x==v[pozx-j]){
r=pozx-j;
j++;
}
fout<<r<<'\n';
}
else{
j=N;
while(x<v[j]){
r=j;
j--;
}
fout<<r<<'\n';
}
}
}
return 0;
}