Cod sursa(job #2343465)
| Utilizator | Data | 13 februarie 2019 23:47:48 | |
|---|---|---|---|
| Problema | Cautare binara | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.36 kb |
#include<fstream>
#include<algorithm>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,i,j,v[100010],t,st,putere,x,y;
int main(){
fin>>n;
for(i=1; i<=n; i++){
fin>>v[i];
}
sort(v+1,v+n+1);
fin>>t;
for(int i=1;i<=t;i++){
fin>>x>>y;
st=0;
putere=(1<<20);
if(x==0){
while(putere){
if(st+putere<=n){
if(v[st+putere]<=y){
st+=putere;
}
}
putere=putere/2;
}
if(v[st]==y){
fout<<st<<"\n";
}
else{
fout<<-1<<"\n";
}
}
if(x==1){
while(putere){
if(st+putere<=n){
if(v[st+putere]<=y){
st+=putere;
}
}
putere=putere/2;
}
fout<<st<<"\n";
}
if(x==2){
while(putere){
if(st+putere<=n){
if(v[st+putere]<y){
st+=putere;
}
}
putere=putere/2;
}
fout<<st+1<<"\n";
}
}
}
