3 solutions

  • 1

    二分,简单易懂

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 1e5+10;
    
    int n,m;
    int a[N];
    
    int main(){
        cin >> n >> m;
        for(int i = 0;i < n;i++){
            cin >> a[i];
        }
        sort(a,a+n);
        while(m--){
            int w;
            cin >> w;
            int l = 0,r = n;
            while(l < r){
                int mid = (l+r)/2;
                if(a[mid]<=w){
                    l = mid+1;
                }else{
                    r = mid;
                }
            }
            cout << l << endl;
        }
        return 0;
    }
    
    

    Information

    ID
    95
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    (None)
    # Submissions
    262
    Accepted
    24
    Uploaded By