DongXD @ 2024-10-21 23:39:10
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int n, m,num[1000005]={0},que;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&num[i]);
}
for(int i=1;i<=m;i++){
scanf("%d",&que);
int now=1,l=n;
bool flag=true;
while(flag){
int mid=(l-now+1)/2+now;
if(num[mid]==que){
for(int i=mid;i>=1;i--){
if(num[i]>num[i-1]){
printf("%d ",i);
flag=false;
break;
}
}
}
if(num[mid]>que&&flag){
if(num[mid-1]<que){
printf("%d ",-1);
flag=false;
break;
}
l-=mid;
continue;
}
else if(flag){
if(num[mid+1]>que){
printf("%d ",-1);
flag=false;
break;
}
now=mid+1;
continue;
}
}
}
cout<<endl;
return 0;
}
by tangzirui1016 @ 2024-10-21 23:48:49
@DongXD lower_bound
了解一下
by tangzirui1016 @ 2024-10-21 23:51:47
@DongXD 二分没学好吧,建议再学一下
by wangzihan__sb @ 2024-10-22 06:31:51
map大法好
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int n,m,a;
signed main() {
ios::sync_with_stdio(0);
cin>>n>>m;
for (int i=1;i<=n;i++){
cin>>a;
if (!mp[a])mp[a]=i;
}
while (m--){
cin>>a;
if (!mp[a])cout<<-1<<" ";
else {
cout<<mp[a]<<" ";;
}
}
return 0;
}