CodeForces 600B Queries about less or equal elements

题目链接:CodeForces 600B Queries about less or equal elements

题目大意:
给定两个整数数组(a)(b)。对于第二个数组的每个元素(b_j),您应该找到数组(a)中小于或等于值(b_j)的元素数。

题解:
排序后二分查找,用(upper\_bound)会方便很多。

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
    int n, m, a[200010], b[200010];
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= m; ++i) {
        cin >> b[i];
    }
    for (int i = 1; i <= m; ++i) {
        int pos = upper_bound(a + 1, a + 1 + n, b[i]) - a - 1;
        cout << pos << ' ';
    }
    return 0;
}
原文地址:https://www.cnblogs.com/IzumiSagiri/p/15220725.html