Pagini recente » Cod sursa (job #805329) | Cod sursa (job #782005) | Rating Gaus Gonnadi (gonnadi) | Rating Olariu Maria Teodora (SnowyMary) | Cod sursa (job #802063)
Cod sursa(job #802063)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
#define nmax 100005
int n;
int a[nmax];
void citeste(){
f >> n;
for(int i=1; i<=n; ++i){
f >> a[i];
}
}
int cb1(int val){
int st = 1;
int dr = n;
int sol = 0;
while(st <= dr){
int mij = (st + dr) / 2;
if (a[mij] == val){
sol = mij;
st = mij + 1;
}
if (a[mij] > val) dr = mij - 1;
if (a[mij] < val) st = mij + 1;
}
return sol;
}
int cb2(int val){
int st = 1;
int dr = n;
int sol = 0;
while(st <= dr){
int mij = (st + dr) /2;
if (a[mij] <= val){
sol = mij;
st = mij + 1;
}else dr = mij - 1;
}
return sol;
}
int cb3(int val){
int st = 1;
int dr = n;
int sol = 0;
while(st <= dr){
int mij = (st + dr) / 2;
if (a[mij] >= val){
sol = mij;
dr = mij - 1;
}else st = mij + 1;
}
return sol;
}
void rezolva(){
int k;
f >> k;
for(; k; --k){
int x, y;
f >> x >> y;
if (x == 0){//cea mai mare pozitie pe care se afla y sa -1 altfel
int c = cb1(y);
if (c != 0) g << c << "\n";
else g << -1 << "\n";
}
if (x == 1){//cea mai mare pozitie a. i. valorea sa fie <= cu y// se paote tot timpul
g << cb2(y) << "\n";
}
if (x == 2){// cea mai mica pozitie a.i valoarea ei sa fie >= cu y//
g << cb3(y) << "\n";
}
}
}
int main(){
citeste();
rezolva();
}