Queries about less or equal elements CodeForces - 600B 排序+二分

题意

  给定两个数组,对b数组中的每一个数b[j]找到a数组中小于等于b[j]的数的个数;

 思路

先将a数组排序,然后二分查找

当时根本不知道有 upper_bound 这个函数   自己写了一个二分由于加了很多循环 所以超时

upper_bound  的用法

https://blog.csdn.net/qq_40160605/article/details/80150252

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+3;
ll a[maxn],b[maxn];  //int 也能过
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%lld",&a[i]);  
    for(int i=0;i<m;i++)
        scanf("%lld",&b[i]);
    sort(a,a+n);
    for(int i=0;i<m;i++)
    {
        if(i==m-1)
            printf("%d\n",upper_bound(a,a+n,b[i])-a);
        else
            printf("%d ",upper_bound(a,a+n,b[i])-a);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41837216/article/details/82498324
今日推荐