Pagini recente » Cod sursa (job #1641633) | Info Oltenia 2018 Proba pe Echipe Clasele 9 - 10 | Cod sursa (job #218877) | Cod sursa (job #1882590) | Cod sursa (job #1611843)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{int n,i,ok=0,st,dr,mij,cod,x,k,c;
long a[100001],m;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
f>>n;
for(i=1; i<=n; i++)
f>>a[i];
f>>m;
for(i=1; i<=m; i++)
{f>>cod;
f>>x;
if(cod==0)
{st=1; dr=n;
while(st<=dr && ok==0)
{mij=(st+dr)/2;
if(a[mij]==x)
ok=1;
else if(a[mij]>x)
dr=mij-1;
else st=mij+1;}
k=mij+1;
while(a[k]==x && k<=n)
k++;
g<<k-1<<endl;}
if(cod==1)
{st=1; dr=n; ok=0;
while(st<=dr && ok==0)
{mij=(st+dr)/2;
if(a[mij]==x)
ok=1;
else if(a[mij]>x)
dr=mij-1;
else st=mij+1;}
c=mij;
cout<<"mij= "<<mij<<endl;
if(ok==1)
{while(a[c]==x && c<=n) c++;
cout<<"c= "<<c<<endl;
if(c<=n) g<<c-1<<endl;
else g<<n;}
else g<<-1;
}
if(cod==2)
{st=1; dr=n; ok=0;
while(st<=dr && ok==0)
{mij=(st+dr)/2;
if(a[mij]==x)
ok=1;
else if(a[mij]>x)
dr=mij-1;
else st=mij+1;}
c=mij;
cout<<"mij= "<<mij<<endl;
if(ok==1)
{while(a[c]==x && c<=n) c--;
cout<<"c= "<<c<<endl;
if(c<=n) g<<c+1<<endl;
else g<<n;}
else g<<-1;
}
}
return 0;
}