Pagini recente » Cod sursa (job #2854661) | Cod sursa (job #2668628) | Cod sursa (job #74912) | Cod sursa (job #2865638) | Cod sursa (job #2851679)
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n, v[100001];
int cautare(int x)
{
int st=1;
int dr=n;
int mij;
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]>x)
{
dr=mij-1;
}
else
{
st=mij+1;
}
}
mij=(st+dr)/2;
if(v[mij]>x)
{
mij--;
}
if(v[mij]==x)
{
return mij;
}
return -1;
}
int caut(int x)
{
int st=1;
int dr=n;
int mij;
while(st<dr)
{
mij=(st+dr)/2;
if(x>=v[mij])
{
st=mij+1;
}
else
{
dr=mij;
}
}
mij=(st+dr)/2;
if(v[mij]>x)
{
mij--;
}
return mij;
}
int caut2(int x)
{
int st=1;
int dr=n;
int mij;
while(st<dr)
{
mij=(st+dr)/2;
if(v[mij]<x)
{
st=mij+1;
}
else
{
dr=mij;
}
}
mij=(st+dr)/2;
if(v[mij]<x){mij++;}
return mij;
}
int m, k, x;
int main()
{
fin>>n;
for(int i=1; i<=n; i++)
{
fin>>v[i];
}
fin>>m;
for(int i=1; i<=m; i++)
{
fin>>k>>x;
if(k==0)
{
fout<<cautare(x)<<"\n";
}
else if(k==1)
{
fout<<caut(x)<<"\n";
}
else if(k==2)
{
fout<<caut2(x)<<"\n";
}
}
return 0;
}