Pagini recente » Cod sursa (job #2005147) | Monitorul de evaluare | Profil M@2Te4i | Cod sursa (job #223476) | Cod sursa (job #382043)
Cod sursa(job #382043)
#include<fstream.h>
#include <iostream.h>
#include <stdio.h>
int main()
{
int max=0,st,dr,mij,poz,N,v[1000],x,i,E,y,z,M;
freopen("cautbin.in", "r",stdin);
freopen("cautbin.out", "w",stdout);
cin>>N;
for(i=1;i<=N;i++)
cin>>v[i];
cin>>M;
dr=N+1;
st=0;
mij=(dr+st)/2;
for(;M>0;M--)
{cin>>E;
cin>>x;
while(st+1<dr)
{
if(x<v[mij])
{dr=mij;
mij=(dr+st)/2;
}
if(x>=v[mij])
{st=mij;
mij=(dr+st)/2;
}
}
if(E==0)
{if(x==v[mij])
{
while(x==v[mij+1])
{
mij=mij+1;
}
cout<<mij<<endl;
}
else
cout<<-1<<endl;
}
if(E==1)
{
if(x==v[mij])
{
while(x==v[mij+1])
{
mij=mij+1;
}
cout<<mij<<endl;
}
else
{
y=v[st];
while(y==v[st+1])
st=st+1;
cout<<st<<endl;
}
}
if(E==2)
{
if(x==v[mij])
{while(x==v[mij-1])
mij=mij-1;
cout<<mij<<endl;
}
else
{
z=v[dr];
while(z==v[dr-1])
dr=dr-1;
cout<<dr<<endl;
}
}
}
return 0;
}