Pagini recente » Cod sursa (job #1601615) | Cod sursa (job #718947) | Cod sursa (job #74097) | Cod sursa (job #79536) | Cod sursa (job #2810682)
#include <iostream>
using namespace std;
void firstCase(int n, int v[], int x) {
int left = 1;
int right = n + 1;
while (left < right) {
int middle = (left + right) / 2;
if (v[middle] <= x) {
++left;
} else {
right = middle;
}
}
if (v[left - 1] == x) {
cout << left - 1;
} else {
cout << -1;
}
}
void secondCase(int n, int v[], int x) {
int left = 1;
int right = n + 1;
while (left < right) {
int middle = (left + right) / 2;
if (v[middle] <= x) {
++left;
} else {
right = middle;
}
}
if (v[left - 1] <= x) {
cout << left - 1;
}
}
void thirdCase(int n, int v[], int x) {
int left = 1;
int right = n;
while (left < right) {
int middle = (left + right) / 2;
if (v[middle] < x) {
++left;
} else {
right = middle;
}
}
if (v[left] >= x) {
cout << left;
}
}
int main() {
int n, v[100001];
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> v[i];
}
int cases;
cin >> cases;
for (int i = 1; i <= cases; ++i) {
int currentCase, x;
cin >> currentCase >> x;
if (currentCase == 0) {
firstCase(n, v, x);
} else if (currentCase == 1) {
secondCase(n, v, x);
} else {
thirdCase(n, v, x);
}
cout << "\n";
}
}