codeforces_600B. Queries about less or equal elements(排序二分)

来源:互联网 发布:mac mpv播放器 慢放 编辑:程序博客网 时间:2024/06/09 19:53
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 array athat 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.

Examples
input
5 41 3 5 7 96 4 2 8
output
3 2 1 4
input
5 51 2 1 2 53 1 4 1 5
output
4 2 4 2 5
很容易想到排序后二分,不过我们平时写的二分不一定是满足题目中要得到最后一个大于等于查询值的位置的要求。
所以我们可以参考STL的源码来修改我们的二分,当然我们也可以直接用STL中的upper_bound(int *array,int size,int key)来解决,我用的修改后的二分。
以后如果想得到第一个大于等于查询值的位置,相应的可以用STL中的lower_bound()。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int a[200010];int search(int l,int r,int key){    int mid,pos=0;    while(l<r)    {        mid=l+r>>1;        if(a[mid]>key)        {            r=mid;            pos=r;        }        else        {            l=mid+1;            pos=l;        }    }    return pos;}int main(){    int n,m;    Si(n);    Si(m);    int i;    for(i=0;i<n;i++)    {        Si(a[i]);    }    sort(a,a+n);    for(i=1;i<=m;i++)    {        int b;        Si(b);        int ans=search(0,n,b);        printf("%d",ans);        if(i!=m)printf(" ");    }    return 0;}


附上仿STL两个函数的代码
//这个算法中,first是最终要返回的位置int lower_bound(int *array, int size, int key){    int first = 0, middle;    int half, len;    len = size;    while(len > 0) {        half = len >> 1;        middle = first + half;        if(array[middle] < key) {                 first = middle + 1;                      len = len-half-1;       //在右边子序列中查找        }        else            len = half;            //在左边子序列(包含middle)中查找    }    return first;}

int upper_bound(int *array, int size, int key){    int first = 0, len = size-1;    int half, middle;    while(len > 0){        half = len >> 1;        middle = first + half;        if(array[middle] > key)     //中位数大于key,在包含last的左半边序列中查找。            len = half;        else{            first = middle + 1;    //中位数小于等于key,在右半边序列中查找。            len = len - half - 1;        }    }    return first;}


0 0