Pagini recente » Cod sursa (job #2918659) | Cod sursa (job #1014299) | Cod sursa (job #52425) | Cod sursa (job #233615) | Cod sursa (job #2493346)
#include <bits/stdc++.h>
using namespace std;
int v[100001];
int caut_bin1(int st, int dr, int element)
{
int mijloc,raspuns = -1;
if(v[dr]==element)
return dr;
while(st<dr)
{
mijloc = (st+dr)/2;
if(v[mijloc]>element)
dr = mijloc;
else{
st = mijloc+1;
if(v[mijloc]==element)
raspuns = mijloc;
}
}
return raspuns;
}
int caut_bin2(int st, int dr, int element){
int mijloc;
if(v[dr]<=element)
return dr;
while(st<dr){
mijloc = (st+dr)/2;
if(v[mijloc]<=element)
st = mijloc+1;
else dr= mijloc;
}
if(v[mijloc]>element)
return mijloc-1;
return mijloc;
}
int caut_bin3(int st,int dr,int element){
int mijloc;
while(st<dr){
mijloc = (st+dr)/2;
if(v[mijloc]<element)
st = mijloc+1;
else
dr = mijloc;
}
if(v[mijloc]<element)
return mijloc+1;
return mijloc;
}
int main()
{
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n,x,m,t;
fin>>n;
for(int i=1; i<=n; i++)
fin>>v[i];
fin>>m;
while(m){
fin>>t>>x;
if(t==0)
fout<<caut_bin1(1,n,x)<<'\n';
if(t==1)
fout<<caut_bin2(1,n,x)<<'\n';
if(t==2)
fout<<caut_bin3(1,n,x)<<'\n';
m--;
}
return 0;
}