Educational Codeforces Round 2 B.Queries about less or equal elements(排序&&二分)

Educational Codeforces Round 2B:http://codeforces.com/contest/600/problem/B

B. Queries about less or equal elements
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in arraya that are less than or equal to the value bj.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arrays a and b.

The second line contains n integers — the elements of array a ( - 109 ≤ ai ≤ 109).

The third line contains m integers — the elements of array b ( - 109 ≤ bj ≤ 109).

Output

Print m integers, separated by spaces: the j-th of which is equal to the number of such elements in array a that are less than or equal to the value bj.

Sample test(s)
input
5 4
1 3 5 7 9
6 4 2 8
output
3 2 1 4
input
5 5
1 2 1 2 5
3 1 4 1 5
output
4 2 4 2 5

题目大意:对b数组中的每一个数b[j]找到a数组中小于等于b[j]的数的个数

大致思路:对a数组排序,用二分找第一个大于等于b[j]的数,下标就是所求个数

这么简单的题,一个月前第一次看竟然没有反应过来


#include <cstdio>
#include <algorithm>

using namespace std;

int a[200005],n,m,i,b;

int main() {
    while(2==scanf("%d%d",&n,&m)) {
        for(i=0;i<n;++i)
            scanf("%d",a+i);
        sort(a,a+n);
        a[n++]=0x3f3f3f3f;
        scanf("%d",&b);
        printf("%d",upper_bound(a,a+n,b)-a);
        while(--m) {
            scanf("%d",&b);
            printf(" %d",upper_bound(a,a+n,b)-a);
        }
        printf("\n");
    }
    return 0;
}



你可能感兴趣的:(排序,codeforces,二分)