chht_0219 @ 2024-05-29 13:28:30
#include<bits/stdc++.h>
using namespace std;
int a[10010],n,x[10000],m,i;
int fz(int x){
int lt=1,rt=n,md=0;
while(lt<=rt){
md=(lt+rt)/2;
if(a[md]==x)return md;
if(a[md]>x)rt=md-1;
else lt=md+1;
}
return -1;
}
int main(){
cin>>n;cin>>m;
for( i=1;i<=n;i++)cin>>a[i];
for( i=1;i<=m;i++)cin>>x[i];
for( i=1;i<=m;i++)cout<<fz(x[i])<<" ";
return 0;
}
by HuangBarry @ 2024-05-29 13:36:05
@chht_0219 数组开小了
by chht_0219 @ 2024-05-29 13:59:21
#include<bits/stdc++.h>
using namespace std;
int a[1000010],n,x[1000000],m,i;
int fz(int x){
int lt=1,rt=n,md=0;
while(lt<rt){
md=(lt+rt)/2;
if(a[md]>=x)rt=md;
else lt=md+1;
}
if(a[lt]==x)return lt;
else return -1;
}
int main(){
cin>>n;cin>>m;
for( i=1;i<=n;i++)cin>>a[i];
for( i=1;i<=m;i++){cin>>x[i];cout<<fz(x[i])<<" ";
}
return 0;
}
AC