Zxm2013 @ 2024-10-25 21:40:58
手打二分RE了,qwq
#include<cstdio>
int n,m,a[10000010];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
int mid;
for(int i=1;i<=m;i++){
bool t=true;
int f;
scanf("%d",&f);
int y=1,x=f;
int j=0;
while(y<=x&&j<n){
j++;
mid=(y+x)/2;
if(a[mid]==f){
t=false;
printf("%d ",mid);
break;
}
else if(a[mid]<f){
x=mid-1;
}
else y=mid+1;
}
if(t){
printf("-1");
}
}
return 0;
}
by mmnBilibili @ 2024-11-02 10:56:49
#define int long long
int a[1000100];
int n, m, x;
int check(int x) {
int l = 1, r = n;
while (l < r) {
int mid = (l + r) / 2;
if (a[mid] >= x) {
r = mid;
}
else {
l = mid + 1;
}
}
if (a[l] == x||a[r]==x) return l;
else return -1;
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
while (m--) {
cin >> x;
printf("%lld ", check(x));
}
return 0;
}