Pagini recente » Cod sursa (job #1300334) | Cod sursa (job #2677119) | Cod sursa (job #3275249) | Cod sursa (job #2404942) | Cod sursa (job #3037747)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int v[100010];
int cautare0(int st, int dr, int x)
{
if(st>dr)return -1;
else
{
int m=st+(dr-st)/2;
if(v[m]==x && v[m+1]==x)
return cautare0(m+1,dr,x);
else if(v[m]==x && v[m+1]!=x)return m;
else if(x<v[m])return cautare0(st,m-1,x);
else
return cautare0(m+1,dr,x);
}
}
int cautare1(int st, int dr, int x)
{
if(st==dr)return dr;
else{
int m=st+(dr-st)/2;
if(v[m]==v[dr])return dr;
else if(v[m]<=x && v[m+1]>x)return m;
else if(v[m]<=x)
return cautare1(m+1,dr,x);
else if(v[m]>x)return cautare1(st,m-1,x);
}
}
int cautare2(int st, int dr, int x)
{
if(st==dr)return dr;
else{
int m=st+(dr-st)/2;
if(v[m]>=x && v[m-1]<x)return m;
else if(v[m]>=x)cautare2(st,m-1,x);
else
cautare2(m+1,dr,x);
}
}
int main()
{
int n,m,x,intrebare;
fin>>n;
for(int i=1;i<=n;i++)fin>>v[i];
fin>>m;
int a;
for(int i=1;i<=m;i++)
{
fin>>intrebare>>x;
if(intrebare==0)
{
a=cautare0(1,n,x);
fout<<a<<'\n';
}
if(intrebare==1)fout<<cautare1(1,n,x)<<'\n';
if(intrebare==2)fout<<cautare2(1,n,x)<<'\n';
}
return 0;
}