CF1073B Vasya and Books(队列)

题目链接

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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章