Pagini recente » Cod sursa (job #3160997) | Cod sursa (job #2883998) | Cod sursa (job #3186735) | Cod sursa (job #2697355) | Cod sursa (job #2044923)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int n, m, x, i, v[100001], p, a, nmax=-1, nmin=100006;
int caut0(int s, int d)
{
if(s>d)
return nmax;
else
{
m=(s+d)/2;
if(v[m]==x)
{
nmax=max(m,nmax);
caut0(m+1,d);
}
else if(x>v[m])
caut0(s,m-1);
else caut0(m+1,d);
}
}
int caut1(int s, int d)
{
if(s>d)
return nmax;
else
{
m=(s+d)/2;
if(v[m]<=x)
{
nmax=max(m,nmax);
caut1(m+1,d);
}
else caut1(s,m-1);
}
}
int caut2(int s, int d)
{
if(s>d)
return nmin;
else
{
m=(s+d)/2;
if(v[m]>=x)
{
nmin=min(m,nmin);
caut2(s,m-1);
}
else caut2(m+1,d);
}
}
int main()
{
f>>n;
for(i=1;i<=n;i++)
f>>v[i];
f>>p;
for(i=1;i<=p;i++)
{
nmax=-1; nmin=100006
f>>a>>x;
if(a==0)
g<<caut0(1,n)<<"\n";
else if(a==1)
g<<caut1(1,n)<<"\n";
else
g<<caut2(1,n)<<"\n";
}
}