欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

codeforces 1073B. Vasya and Books

程序员文章站 2022-04-02 22:25:57
...

B. Vasya and Books
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya has got nn books, numbered from 11 to nn, arranged in a stack. The topmost book has number a1a1, the next one — a2a2, and so on. The book at the bottom of the stack has number anan. All numbers are distinct.

Vasya wants to move all the books to his backpack in nn steps. During ii-th step he wants to move the book number bibi into his backpack. If the book with number bibi is in the stack, he takes this book and all the books above the book bibi, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1,2,3][1,2,3] (book 11 is the topmost), and Vasya moves the books in the order [2,1,3][2,1,3], then during the first step he will move two books (11 and 22), during the second step he will do nothing (since book 11 is already in the backpack), and during the third step — one book (the book number 33). Note that b1,b2,…,bnb1,b2,…,bn are distinct.

Help Vasya! Tell him the number of books he will put into his backpack during each step.

Input
The first line contains one integer n (1≤n≤2⋅105)n (1≤n≤2⋅105) — the number of books in the stack.

The second line contains nn integers a1,a2,…,an (1≤ai≤n)a1,a2,…,an (1≤ai≤n) denoting the stack of books.

The third line contains nn integers b1,b2,…,bn (1≤bi≤n)b1,b2,…,bn (1≤bi≤n) denoting the steps Vasya is going to perform.

All numbers a1…ana1…an are distinct, the same goes for b1…bnb1…bn.

Output
Print nn integers. The ii-th of them should be equal to the number of books Vasya moves to his backpack during the ii-th step.

Examples
inputCopy
3
1 2 3
2 1 3
outputCopy
2 0 1
inputCopy
5
3 1 4 2 5
4 5 1 3 2
outputCopy
3 2 0 0 0
inputCopy
6
6 5 4 3 2 1
6 5 3 4 2 1
outputCopy
1 1 2 0 1 1
Note
The first example is described in the statement.

In the second example, during the first step Vasya will move the books [3,1,4][3,1,4]. After that only books 22 and 55 remain in the stack (22 is above 55). During the second step Vasya will take the books 22 and 55. After that the stack becomes empty, so during next steps Vasya won’t move any books.
题目挺简单的一道题,直接上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxx=2e5+10;
int a[maxx];
int b[maxx];
int n;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		memset(a,0,sizeof(a));
		int k=0;
		for(int i=0;i<n;i++)
		{
			int x;
			scanf("%d",&x);
			a[x]=++k;
		}
		for(int i=0;i<n;i++) scanf("%d",&b[i]);
		int maxn=0;
		for(int i=0;i<n;i++)
		{
			if(a[b[i]]==maxn) cout<<1<<" ";
			else if(a[b[i]]>maxn)
			{
				cout<<a[b[i]]-maxn<<" ";
				maxn=a[b[i]];
			}
			else cout<<0<<" ";
		}
	}
}

努力加油a啊,(o)/~

相关标签: 思维