Codeforces Educational Codeforces Round 2 B. Queries about less or equal elements

来源:互联网 发布:淘宝企业店铺公示地址 编辑:程序博客网 时间:2024/05/20 03:38

题目链接

题意:

a[n] 和 b[m],对于每个b[i], 求a[n]中有几个元素 <= b[i]

思路:

看到<=,第一反应想到lower_bound,但是这是查询第一个小于等于的元素的位置,但是有多个元素等于b[i],就会出错

用upper_bound就不会啦~

代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 2E5 + 10;int n, m;int a[N], b[N];int main(){while(~scanf("%d%d", &n, &m)){for(int i = 0; i < n; i++)scanf("%d", &a[i]);sort(a, a + n);for(int j = 0; j < m; ++j)scanf("%d", &b[j]);for(int i = 0; i < m; i++){if(i == m)printf("%d\n", upper_bound(a ,a + n, b[i]) - a);else printf("%d ",upper_bound(a, a + n, b[i]) - a);} }return 0;} 


0 0
原创粉丝点击