wjz0000000 @ 2024-07-25 09:16:03
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int a[1000000],n,m,mid;
void cha(int x,int l,int r){
while(l<=r){
mid=(l+r)/2;
if(x==a[mid]){
while(a[mid]==a[mid-1]){
mid--;
}cout<<mid+1<<" ";
return ;
}else if(x<a[mid]){
r=mid-1;
}else{
l=mid+1;
}
}cout<<-1;
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[i];
}int x;
for(int i=0;i<m;i++){
cin>>x;
cha(x,0,n);
}
}
by yanghanhang @ 2024-07-25 09:19:08
#include<bits/stdc++.h>
using namespace std;
int n,m,g[1000010];
int check(int x){
int l=1,r=n,ans=0;
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 yanghanhang @ 2024-07-25 09:19:54
试试
by yzlyzl123 @ 2024-08-06 12:47:57
cout<<-1<<" ",-1后面要打空格
by wjz0000000 @ 2024-08-16 08:33:59
谢谢