Pagini recente » Cod sursa (job #1203898) | Cod sursa (job #2718991) | Istoria paginii runda/mehrschwankerweiterte/clasament | Cod sursa (job #2290888) | Cod sursa (job #2717881)
#include <fstream>
# define NM 100001
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int a[NM],n,m,i,c,x;
void cautabinar0()
{
int st,dr,mij,poz=-1;
st=1;
dr=n;
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]==x)
{
poz=mij;
st=mij+1;
}
else
if(a[mij]>x)
dr=mij-1;
else
st=mij+1;
}
fout<<poz<<"\n";
}
void cautabinar1()
{
int st,dr,mij,poz;
st=1;
dr=n;
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]<=x)
{
poz=mij;
st=mij+1;
}
else
dr=mij-1;
}
fout<<poz<<"\n";
}
void cautabinar2()
{
int st,dr,mij,poz;
st=1;
dr=n;
while(st<=dr)
{
mij=(st+dr)/2;
if(a[mij]>=x)
{
poz=mij;
dr=mij-1;
}
else
st=mij+1;
}
fout<<poz<<"\n";
}
int main()
{
fin>>n;
for(i=1;i<=n;i++)
fin>>a[i];
fin>>m;
for(int j=1;j<=m;j++)
{
fin>>c>>x;
switch(c)
{
case 0:
{
cautabinar0();
break;
}
case 1:
{
cautabinar1();
break;
}
default:
{
cautabinar2();
break;
}
}
}
return 0;
}
/**
5
1 3 3 3 5
3
0 3
1 3
2 3
4
4
2
**/