Pagini recente » Cod sursa (job #1589546) | Cod sursa (job #289544) | Cod sursa (job #2192753) | Cod sursa (job #1790976) | Cod sursa (job #1732648)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int v[100000];
int cautbin(int ls, int ld, int x)
{
int mij = ( ls + ld ) / 2;
if(x < mij)
return cautbin(ls, mij - 1, x);
else if(x > mij)
return cautbin( mij + 1, ld, x);
else return mij;
return -1;
}
int main()
{
int n, m;
int i, tip_intrebari, x;
f >> n ;
for(i = 1;i <= n;i++)
f >> v[i];
f >> m;
for(i = 0;i < m;i++)
{
f >> tip_intrebari >> x;
if(tip_intrebari == 0)
{
int gasit = cautbin( 1, n, x);
while(v[gasit + 1] == x)
gasit++;
g << gasit << endl;
}
if(tip_intrebari == 1)
{
int gasit = cautbin(1, n, x);
while(v[gasit + 1] == x)
gasit++;
g << gasit << endl;
}
if(tip_intrebari == 2)
{
int gasit = cautbin(1, n, x);
while(v[gasit - 1] == x)
gasit--;
g << gasit;
}
}
return 0;
}