RE
2024-07-28 17:10:33
发布于:浙江
45阅读
0回复
0点赞
#include<iostream>
using namespace std;
int n,x,a[1000005]={},sum,l,r,m;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=0;i<m;i++){
cin>>x;
sum=-1,l=1,r=n;
while(l<=r){
int mid=(l+r)/2;
if(a[mid]==x){
sum=mid;
}if(a[mid]>=x){
r=mid-1;
}else{
l=mid+1;
}
}
cout<<sum<<" ";
}
}
全部评论 2
超时了,范围太小了
2024-08-19 来自 江苏
0同问
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[10000005]; int main() { ll n; cin>>n; ll t; cin>>t; for(ll i = 1;i<=n;i++) cin>>a[i]; while(t--){ ll k; bool flag = false; cin>>k; ll l = 1; ll r = n; while(l<=r) { ll mid = (l+r)/2; if(a[mid]==k&&a[mid-1]==k||a[mid]>k) { r=mid-1; } else if(a[mid]<k) { l=mid+1; } else { cout<<mid<<" "; flag = true; break; } } if(!flag) { cout<<-1<<" "; } flag=false; } return 0; }
2024-08-03 来自 陕西
0+1
#include<iostream> using namespace std; int main(){ int a,b,arr[9999999],c; cin >> a >> b; for(int i = 0 ; i < a ; i++) cin >> arr[i]; for(int i = 0 ; i < b ; i++){ cin >> c; int temp = -1; for(int i = 0 ; i < a ; i++){ if(arr[i] == c){ temp = i+1; break; } } cout << temp << ' '; } }
2024-08-11 来自 陕西
0
有帮助,赞一个