C++ 查找年龄范围:高效算法实现
#include
using namespace std;
int main() { int n, q; cin >> n >> q;
vector<int> ages(n);
for (int i = 0; i < n; i++) {
cin >> ages[i];
}
for (int i = 0; i < q; i++) {
int x;
cin >> x;
auto lower = lower_bound(ages.begin(), ages.end(), x);
auto upper = upper_bound(ages.begin(), ages.end(), x);
if (lower == upper) {
cout << "\-1 \-1" << endl;
} else {
cout << lower - ages.begin() + 1 << " " << upper - ages.begin() << endl;
}
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/quIX 著作权归作者所有。请勿转载和采集!