Pagini recente » Cod sursa (job #2019689) | Cod sursa (job #2141784) | Rating Bratu Gabriel (Gaby) | Istoria paginii runda/cerculetz_01/clasament | Cod sursa (job #2354595)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cautbin.in") ;
ofstream fout("cautbin.out") ;
unsigned int n , m , v[100005] , a , b , x ;
int caut(int l , int r , int x)
{
int mij = 1 ;
while(l <= r)
{
mij = (l + r) / 2 ;
if(v[mij] > x)
r = mij - 1 ;
else if(v[mij] < x)
l = mij + 1 ;
else return mij ;
}
return mij ;
}
int main()
{
fin >> n ;
for(int i=1; i<=n; i++)
fin >> v[i] ;
fin >> m ;
for(int i=1; i<=m; i++)
{
fin >> a >> b ;
x = caut(1 , n , b) ;
while(v[x+1] <= b && x<n)
x ++ ;
if(a == 0)
{
if(v[x] == b)
fout << x << "\n";
else
fout << "-1" << "\n" ;
}
else if(a == 1)
fout << x << "\n";
else
{
while(v[x-1] >= b && x > 1)
x -- ;
if(v[x] >= b)
fout << x << "\n" ;
else fout << x + 1 << "\n" ;
}
}
return 0;
}