样例过了 0分求助!!!
查看原帖
样例过了 0分求助!!!
636507
Qinmoran楼主2022/11/25 20:05
#include<iostream>
using namespace std;
const int N = 1e6+100;
int a[N],n;
void find(int x){
    int l = 1 , r = n;
    int m;
    while(l<r){
        m = ( l + r ) >> 1;
    //    cout << m << endl;
        if(a[m]>=x){
            r = m - 1;
        }
        else{
            l = m + 1;
        }
    }
    if(a[l]==x){
        cout << l << ' ';
    }
    else 
    cout << -1 << ' ';
}
int main(){
    ios:: sync_with_stdio ( false );
    int m,tem;
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i ++ ){
        cin >> a[i];
    }
    while(m--){
        cin >> tem;
        find(tem);
    }
    return 0;
}
2022/11/25 20:05
加载中...