Pagini recente » Cod sursa (job #2231986) | Cod sursa (job #414188) | Cod sursa (job #2562594) | Cod sursa (job #1802201) | Cod sursa (job #653160)
Cod sursa(job #653160)
#include<fstream>
#include<iostream>
using namespace std;
int a[1000001],n,m;
int cauta1(int lol,int f,int l){
int me=(f+l)/2;
if(f==l && a[me]!=lol)
return -1;
if(f==l-1){
return -1;
}
if(a[me]==lol)
return me;
else
if(lol>a[me])
return cauta1(lol,me,l);
else
return cauta1(lol,f,me);
}
int cauta2(int lol,int f,int l){
int me=(f+l)/2;
if(a[me]<=lol && a[me+1]>=lol)
return me;
else{
if(f==l-1){
return l;
}
if(lol>a[me])
return cauta2(lol,me,l);
else
return cauta2(lol,f,me);
}
}
int main(){
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>n;
int i;
for(i=1;i<=n;i++){
f>>a[i];
}
a[n+1]=9999999;
f>>m;
int x,y;
for(i=1;i<=m;i++){
f>>x>>y;
if(x==0){
x=cauta1(y,1,n);
while(a[x]==y){
x++;
}
if(x==-1){
g<<x<<"\n";
}
else
g<<x-1<<'\n';
}
else
if(x==1){
x=cauta2(y,1,n);
while(a[x]<=y && x<=n){
x++;
}
if(x==-1){
g<<x<<"\n";
}
else
g<<x-1<<'\n';
}
else{
x=cauta2(y,1,n);
while(a[x]<y && x<=n){
x++;
}
if(x>n)
x=n;
g<<x<<'\n';
}
}
return 0;
}