Pagini recente » Cod sursa (job #1940055) | Cod sursa (job #2444528) | Cod sursa (job #2323643) | Cod sursa (job #587490) | Cod sursa (job #1596638)
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[100000],n;
int cautbin1(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-1;
}
return dr;
}
int cautbin0(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-1;
}
if(v[mij]==x)
return dr;
else return -1;
}
int cautbin2(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-1;
}
return st;
}
int main()
{
ios_base::sync_with_stdio(false);
int i,M,caz,ind;
f>>n;
for(i=1;i<=n;i++)
f>>v[i];
f>>M;
while(M)
{
f>>caz;
f>>ind;
if(caz==0)
g<<cautbin0(ind);
if(caz==1)
g<<cautbin1(ind);
if(caz==2)
g<<cautbin2(ind);
g<<'\n';
M--;
}
return 0;
}