Pagini recente » Cod sursa (job #1885789) | Cod sursa (job #2436812)
#include <fstream>
using namespace std;
ifstream fin ("cautbin.in");
ofstream fout ("cautbin.out");
int v[100001], n, i, intrebari, x, tip;
int cautbinar1(int x)
{
int st=1;
int dr=n;
int nr=-1, mij;
while(st<=dr)
{
mij=(dr+st)/2;
if(v[mij]==x)
{
nr=mij;
st=mij+1;
}
else if(v[mij]>x)
dr=mij-1;
else if(v[mij]<x)
st=mij+1;
}
return nr;
}
int cautbinar2(int x)
{
int st=1;
int dr=n;
int nr, mij;
while(st<=dr)
{
mij=(dr+st)/2;
if(v[mij]<=x)
{
st=mij+1;
nr=mij;
}
else dr=mij-1;
}
return nr;
}
int cautbinar3(int x)
{
int st=1;
int dr=n;
int nr, mij;
while(st<=dr)
{
mij=(st+dr)/2;
if(v[mij]>=x)
{
dr=mij-1;
nr=mij;
}
else st=mij+1;
}
return nr;
}
int main()
{
fin >> n;
for(i=1;i<=n;i++)
fin >> v[i];
fin >> intrebari;
for(i=1;i<=intrebari;i++)
{
fin >> tip >> x;
if(tip==0)
fout << cautbinar1(x) << '\n';
else if(tip==1)
fout << cautbinar2(x) << '\n';
else if(tip==2)
fout << cautbinar3(x) << '\n';
}
return 0;
}