Pagini recente » Cod sursa (job #86119) | Cod sursa (job #338035) | Cod sursa (job #939184) | Cod sursa (job #1897121) | Cod sursa (job #2668285)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
const int nmax=100000;
int n,v[nmax+1];
int caut0(int x)
{
int p=1,u=n,poz=-1;
while(p<=u)
{
int m=(p+u)/2;
if(x==v[m])
{
poz=m;
p=m+1;
}
else
if(x>v[m])p=m+1;
else u=m-1;
}
return poz;
}
int caut1(int x)
{
int p=1,u=n,poz=-1;
while(p<=u)
{
int m=(p+u)/2;
if(x>=v[m])
{
poz=m;
p=m+1;
}
else
u=m-1;
}
return poz;
}
int caut2(int x)
{
int p=1,u=n,poz=-1;
while(p<=u)
{
int m=(p+u)/2;
if(x<=v[m])
{
poz=m;
u=m-1;
}
else
p=m+1;
}
return poz;
}
int main()
{
int m,x,l,t;
f>>n;
for(int i=1; i<=n; i++)
f>>v[i];
f>>m;
while(m--)
{
f>>t>>x;
switch(t)
{
case 0:
g<<caut0(x)<<'\n';
break;
case 1:
g<<caut1(x)<<'\n';
break;
//default:
case 2:
g<<caut2(x)<<'\n';
//break;
}
}
return 0;
}