Pagini recente » Cod sursa (job #739871) | Cod sursa (job #1925153) | Cod sursa (job #3204184) | Cod sursa (job #1332829) | Cod sursa (job #3233208)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int find_last_position(const vector<int>& arr, int x) {
auto it = upper_bound(arr.begin(), arr.end(), x);
if (it == arr.begin()) return -1;
--it;
if (*it == x) return distance(arr.begin(), it);
return -1;
}
int find_last_leq_position(const vector<int>& arr, int x) {
auto it = upper_bound(arr.begin(), arr.end(), x);
if (it == arr.begin()) return -1;
--it;
return distance(arr.begin(), it);
}
int find_last_geq_position(const vector<int>& arr, int x) {
auto it = lower_bound(arr.begin(), arr.end(), x);
if (it == arr.end()) return -1;
return distance(arr.begin(), it);
}
int main() {
ifstream infile("cautbin.in");
ofstream outfile("cautbin.out");
if (!infile || !outfile) {
cerr << "Error opening file" << endl;
return 1;
}
int N, M;
infile >> N;
vector<int> arr(N);
for (int i = 0; i < N; ++i) {
infile >> arr[i];
}
infile >> M;
for (int i = 0; i < M; ++i) {
int op, x;
infile >> op >> x;
if (op == 0) {
outfile << find_last_position(arr, x) + 1 << endl;
} else if (op == 1) {
outfile << find_last_leq_position(arr, x) + 1 << endl;
} else if (op == 2) {
outfile << find_last_geq_position(arr, x) + 1 << endl;
}
}
infile.close();
outfile.close();
return 0;
}