AcWing 5151. 程序调用
原题链接
中等
作者:
comএ涂山鸿鸿
,
2023-09-20 01:28:27
,
所有人可见
,
阅读 30
哈希表
C++ 代码
#include<iostream>
#include<unordered_map>
using namespace std;
using intll = long long;
const intll N =100010;
int a[N];
int n,m,k;
unordered_map<int,int>h;
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
h[a[i]]=i;
}
long long ans=0;
while(m--){
int x;
cin>>x;
if(h[x]%k==0)ans+=h[x]/k;
else ans+=h[x]/k+1;
if(h[x]>1){
int t=h[x];
swap(h[x],h[a[t-1]]); //必须在前面 如果先交换a[t]和a[t-1]的话,a[t-1]上放的是a[t]的值
//然后再交换h[x]和h[a[t-1]]等于交换h[x]和h[a[t]] 但是a[t] = x
//所以交换无效,错误!!!!
swap(a[t],a[t-1]);
}
}
cout<<ans<<endl;
return 0;
}