Pagini recente » Cod sursa (job #3272665) | Cod sursa (job #2975934) | Cod sursa (job #2266800) | Cod sursa (job #2278338) | Cod sursa (job #2272319)
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int main()
{
int i,n,t,j,st,dr,mij,x,op;
fin >> n;
int v[n+1];
for (i=1;i<=n;i++)
fin >> v[i];
fin >> t;
int out[t+1];
for (j=1;j<=t;j++) {
fin >> op>>x;
st=1;
dr=n;
mij=(st+dr)/2;
if (op==0) {
while (st!=dr && st!=mij ) {
if (v[mij]<=x)
st=mij;
else
dr=mij;
mij=(st+dr)/2;
}
if (v[st]==x)
out[j]=st;
else
out[j]=-1;
}
if (op==1) {
while (st!=dr && st!=mij ) {
if (v[mij]<=x)
st=mij;
else
dr=mij;
mij=(st+dr)/2;
}
out[j]= st;
}
if (op==2) {
while (st!=dr && st!=mij) {
if (v[mij]>=x)
dr=mij;
else
st=mij;
mij=(st+dr)/2;
}
out[j]=dr;
}
}
for (j=1;j<=t;j++)
fout << out[j]<<endl;
return 0;
}