Sos_Dou @ 2024-04-20 22:59:10
#include<bits/stdc++.h>
using namespace std;
int n,m,g[1000010];
int check(int x){
int l=1,r=n,ans;
while(l<=r){
int mid=(l+r)/2;
if(g[mid]>=x){
r=mid-1;
ans=mid;
}
else l=mid+1;
}
if(g[ans]==x) return ans;
else return -1;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>g[i];
}
for(int i=1;i<=m;i++){
int x;
cin>>x;
cout<<check(x)<<" ";
}
return 0;
}
by Sos_Dou @ 2024-04-21 14:32:38
第五行改成ans=0 全ac