Sirius_H2SO4 @ 2024-03-16 16:05:26
#include<bits/stdc++.h>
using namespace std;
int BNRYSRCH(int n[],int end,int goal){
int l,r,mid;
l=0;r=end;
while(l<=r){
mid=(l+r)/2;
if(n[mid]==goal){return 1;}
else if(n[mid]<goal)l=mid+1;
else r=mid-1;
}
return 0;
}
int main(){
int n,q;
cin >> n;
cin >> q;
int number[n];
for(int i=0;i<n;i++){
cin >> number[i];
}
int quest;
for(int i=0;i<q;i++){
int count=0;
cin >> quest;
while(BNRYSRCH(number,n-1,quest)){
count++;
}
cout << count << ' ';
}
}
感觉缺了标记已经二分过的选项但是不知道怎么实现,有大佬看看怎么解决以及其他的思路问题吗
by xiechengxuan1061862 @ 2024-03-16 16:09:04
#include<bits/stdc++.h>
using namespace std;
int n,m,a[1000010];
map<int,int> qwq;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
if(!qwq[a[i]]){
qwq[a[i]]=i;
}
}
for(int i=1;i<=m;i++){
int b;
cin>>b;
if(!qwq[b]) cout<<-1<<" ";
else cout<<qwq[b]<<" ";
}
return 0;
}
by xiechengxuan1061862 @ 2024-03-16 16:09:24
感觉没有必要写那么复杂……
by xiechengxuan1061862 @ 2024-03-16 16:11:12
ThisIsSirius_859