全部wa,求助
查看原帖
全部wa,求助
241782
Echo_j楼主2020/6/15 21:35
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn = 1e6+9;
int a[maxn];
int main()
{
    int m,n;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++)
    {
        LL num;
        cin>>num;
        int l = 1;
        int r = n;
        int ans = -1;
        while(l<=r)
        {
            int mid = (r+l)/2;

            if(a[mid]>num) r = mid-1;
            else if(a[mid]<num) l = mid+1;
            else
            {
                ans = mid;
                while(a[mid]==ans)
                {
                        ans = mid;
                        mid--;
                }
                break;
            }
        }
        if(ans!=-1) printf("%d ",ans);
        else printf("-1 ");
    }
    return 0;
}

2020/6/15 21:35
加载中...