您的位置:首页 > 产品设计 > UI/UE

【Educational Codeforces Round 2B】【map or 二分查找】Queries about less or equal elements b[]中的每个数比a[]中多少数大

2015-11-29 20:22 573 查看
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


//http://blog.csdn.net/snowy_smile
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=2e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n,m;
int a
,b
;
map<int,int>mop;
map<int,int>::iterator it;
void binary_search()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(int i=1;i<=m;++i)
{
int x;scanf("%d",&x);
printf("%d ",lower_bound(a+1,a+n+1,x+1)-1-a);
}puts("");
}
int main()
{
binary_search();return 0;
while(~scanf("%d%d",&n,&m))
{
mop.clear();
for(int i=1;i<=n;++i){scanf("%d",&a[i]);++mop[a[i]];}
for(int i=1;i<=m;++i){scanf("%d",&b[i]);if(mop[b[i]]==0);}
int pre=mop.begin()->second;
for(it=++mop.begin();it!=mop.end();++it)
{
pre+=it->second;
it->second=pre;
}
for(int i=1;i<=m;++i)printf("%d ",mop[b[i]]);puts("");
}
return 0;
}
/*
【题意】
给你两个数列a[]与b[],两个数列的长度分别为n(2e5)和m(2e5)。
让你求出,对于b[]中的每个数值,a中有多少个数值是在小于等于它的。

【类型】
map计数 or 二分查找

【分析】
这题可以把所有数值都记在map中,a[]中的权值为1,b[]中的权值为0,
然后记录一个前缀和,并更新b[]的答案,就能AC这道题啦。

当然最好的做法还是把a[]排序,然后对于b的每个询问二分位置,得到答案。
前后两种做法的时间复杂度的AC时间分别是452ms和156ms。

【时间复杂度&&优化】
O(log(n+m)

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息