lihaoran2012 @ 2024-08-18 14:04:00
#include<bits/stdc++.h>
using namespace std;
const long long N=1e8+10;
long long d[N],b[N];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>d[i];
for(int j=1;j<=m;j++)
cin>>b[j];
for(int i=1;i<=m;i++){
int cnt=0,ans=0;
for(int j=1;j<=n;j++){
ans++;
if(b[i]==d[j]){
++cnt;
}
if(ans==m){
break;
}
}
if(cnt==0){
cout<<"-1"<<" ";
}else{
cout<<cnt<<" ";
}
}
return 0;
}
by Emil_ @ 2024-08-18 14:06:17
@lihaoran2012 ac代码:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N=1e6+100;
int n,m,a[N],p;
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=m;i++){
cin>>p;
int ans=lower_bound(a+1,a+1+n,p)-a;
if(p!=a[ans])
cout<<-1<<" ";
else
cout<<ans<<" ";
}
return 0;
}
求关
by 违规用户名K&xs3Z^ @ 2024-08-18 14:06:18
@lihaoran2012 读入数据较多且不小 使用二分和scanf加快速度
#include<bits/stdc++.h>
using namespace std;
int n,a[1114514],l,r,sum,k;
int fun(int l,int r,int sum){
while(l<=r){
int mid=r+l>>1;
if(a[mid]==sum) r=mid-1;
else if(a[mid]<sum) l=mid+1;
else if(a[mid]>sum) r=mid-1;
}
if(a[l]==sum) return l;
else return -1;
}
int main(){
scanf("%d",&n);
scanf("%d",&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=k;i++){
scanf("%d",&sum);
cout<<fun(1,n,sum)<<" ";
}
return 0;
}
by lihaoran2012 @ 2024-08-18 14:18:12
@Emil_ 已关注。