题解
2023-08-24 14:53:40
发布于:广东
13阅读
0回复
0点赞
我就不用upper_bound!(意外内存最优)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
int m;
cin >> m;
sort(nums.begin(), nums.end());
for (int i = 0; i < m; i++) {
int num;
cin >> num;
int left = 0;
int right = n - 1;
int count = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] > num) {
count = n - mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
cout << count << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个