爆0求助
查看原帖
爆0求助
658875
Catcats楼主2022/4/30 12:54

rt,求调,估计是端点问题,太蒻了看不出

样例输出1 3 -1

#include<bits/stdc++.h>
using namespace std;
#define elif else if
int f(int a[],int n,int x){
	int l=0,r=n-1;
	while(l<=r){
		int m=(l+r)/2;
		if(a[m]==x)return m+1;
		elif(a[m]>x)r=m-1;
		else l=m+1;
	}
	return -1;
}
int main(){
	int n,m;
	cin>>n>>m;
	int a[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++){
		int tmp;cin>>tmp;
		cout<<f(a,n,tmp)<<' ';
	}
}
2022/4/30 12:54
加载中...