Pagini recente » Cod sursa (job #133686) | Cod sursa (job #2718933) | Cod sursa (job #2101711) | Cod sursa (job #3122794) | Cod sursa (job #1233700)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
#define MAX 100001
int a[MAX];
int n;
int one(int x)
{
int i, step;
for(step = 1 ; step <= n ; step <<= 1);
for(i = 0 ; step ; step >>= 1)
{
if(i + step <= n && a[i + step] <= x)
i += step;
}
if(a[i] == x)
return i;
return -1;
}
int two(int x)
{
int i, step;
for(step = 1 ; step <=n ; step <<= 1);
for(i = 0 ; step ; step >>= 1)
{
if(i + step <= n && a[i + step] <= x)
i += step;
}
return i;
}
int three(int x)
{
int i, step;
for(step = 1 ; step <= n ; step <<= 1);
for(i = 0 ; step ; step >>= 1)
{
if(step + i <= n && a[i + step - 1] < x)
{
i += step;
}
}
return i;
}
int main()
{
int i, m, x;
fin>>n;
for(i=1;i<=n;i++)
{
fin>>a[i];
}
fin>>m;
while(m--)
{
fin>>i>>x;
if(i==0)
fout<<one(x);
if(i==1)
fout<<two(x);
if(i==2)
fout<<three(x);
fout<<"\n";
}
}