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 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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
- codeforces_600B. Queries about less or equal elements(排序二分)
- codeforces Queries about less or equal elements 二分
- Codeforces-600B Queries about less or equal elements(二分)
- Educational Codeforces Round 2 B.Queries about less or equal elements(排序&&二分)
- Queries about less or equal elements
- 【CodeForces 600B 】Queries about less or equal elements(二分查找)
- Codeforces 600B Queries about less or equal elements(二分查找)
- Educational Codeforces Round 2B. Queries about less or equal elements(二分查找)
- Codeforces 600B Queries about less or equal elements 【离散化去重二分查找 + 树状数组】
- Educational Codeforces Round 2 B. Queries about less or equal elements (二分)
- Codeforces 600B Queries about less or equal elements(二分、upper_bound()的应用)
- codeforces B.Queries about less or equal elements
- Codeforces 600.B Queries about less or equal elements
- cordforces 600B Queries about less or equal elements
- Educational Codeforces Round 2_B. Queries about less or equal elements
- Codeforces Educational Codeforces Round 2 B. Queries about less or equal elements
- Educational Codeforces Round 2 B. Queries about less or equal elements
- 【Educational Codeforces Round 2B】【map or 二分查找】Queries about less or equal elements b[]中的每个数比a[]中多少数大
- spring依赖注入
- Android 通知栏Notification的整合 全面学习
- UICollectionview
- JavaScript学习笔记二
- 1007. 素数对猜想 (20)
- codeforces_600B. Queries about less or equal elements(排序二分)
- Cocos精灵帧缓存
- [大数据]本地文件拷贝到hdfs
- JS 事件基础 拖拽
- Snmp学习总结(二)-安装Snmp
- 搭建SS
- 程序在内存中的分布
- 马云:湖畔大学主讲失败案例
- Cocos缓存的实现