从大到小排个序,二分查找比输入的数大的第一个数,然后这个数的位置下标减一即输出
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 100000;
int a[maxn + 5];
int main()
{
int n, q, tmp;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sort(a + 1, a + n + 1);
cin >> q;
while (q--) {
scanf("%d", &tmp);
int l, r;
l = 1; r = n;
while (l <= r) {
int mid = (l + r) / 2;
if (a[mid] > tmp) {
r = mid - 1;
}
else {
l = mid + 1;
}
}
printf("%d\n", l - 1);
}
return 0;
}