4 solutions

  • 0
    @ 2022-4-5 11:36:49
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N = 100010;
    int a[N];
    int b[N];
    int SR(int x,int l,int r) {
        if(l >= r)
            return -1;
        while(l < r) {
            int mid = l + r + 1  >> 1;
            if(a[mid] <= x) {
                l = mid;
            }
            else r = mid - 1;
        }
        if(a[r]==x)return r;
        return -1;
    }
    int main () {
        int n ;
        cin >> n;
        for (int i = 1; i <= n; i ++) {
            cin >> a[i];
        }
        int m;
        cin >> m;
        for (int i = 1; i <= m ; i ++) {
            cin >> b[i];
        }
        for(int i = 1; i <= m; i ++) {
            cout << SR(b[i],0,n) <<' ';
        }
        return 0;
    }
    

    Information

    ID
    1231
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    7
    Tags
    # Submissions
    342
    Accepted
    90
    Uploaded By