題目鏈接
http://codeforces.com/problemset/problem/1073/B
題意
給定n,長度爲n的數組1,長度爲n的數組2。
按照數組2的順序從數組1中拿值,求每拿一個值需要的步數。如果該值已在包中,步數爲0.
題解
用一個標記數組vis記錄下某個值是否已拿;未拿的話就用cnt去計數拿到該值的步數即可。
AC代碼
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+7;
int a[maxn];
bool vis[maxn];
int main()
{
int n;
while(cin>>n)
{
queue<int> que;
memset(vis,false,sizeof(vis));
for(int i=0;i<n;i++)
{
int x;
cin>>x;
que.push(x);
}
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++)
{
int t=a[i];
if(vis[t]) cout<<0;
else
{
int cnt=0;
while(true)
{
int now=que.front();que.pop();
vis[now]=true;
++cnt;
if(now==t) break;
}
cout<<cnt;
}
if(i==n-1) cout<<endl;
else cout<<" ";
}
}
return 0;
}