题目链接
题意:在a数组中比bi小的元素个数。
思路:暴力会超时,用二分函数upper_bound()、lower_bound()
格式:upper_bound(数组首地址,数组最末元素下标加1,key)-数组首地址
例:int ans=upper_bound(a,a+n,key)-a;
upper_bound():返回第一个大于key值的元素下标。
lower_bound(): 大于等于
头文件:#include
using namespace std;
上代码。
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[200010],b[200010];
int main()
{
int n,m,i,j,p=0;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
sort(a,a+n);
for(j=0;j<m;j++)
{
int num=upper_bound(a,a+n,b[j])-a;
if(p==0)printf("%d",num);
else printf(" %d",num);
p++;
}
printf("\n");
}