laowangwaimaidian @ 2024-12-11 13:51:05
#include <stdio.h>
#define MAX 1000005
int main() {
int n, m;
scanf("%d %d", &n, &m);
int a[MAX];
for (int i = 1; i <=n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
int x;
scanf("%d",&x);
int low = 1;
int high = n ;
int found = 0; // 标记是否找到
while (low <=high) {
int mid = (low + high )/ 2;
if (a[mid] == x) {
found = 1;
while (mid > 1 && a[mid - 1] == x) {
mid--;
}
printf("%d ", mid); // 输出后添加换行
break; // 找到后退出循环
}
else if (a[mid] > x) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
if (!found) {
// 如果未找到,可以选择输出特定信息或不做任何操作
// printf("Not found\n");
printf("%d ", -1);
}
}
return 0;
}
by dg114514 @ 2024-12-11 14:02:00
@laowangwaimaidianSTLyyds
#include<bits/stdc++.h>
using namespace std;
int a[1919810];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
while(m--){
int x;
cin>>x;
int ans=lower_bound(a+1,a+n+1,x)-a;
printf("%d ",(x!=a[ans])?-1:ans);
}
return 0;
}