cf Educational Codeforces Round 177 C. Disappearing Permutation

原题

C. Disappearing Permutation

time limit per test2 seconds

memory limit per test512 megabytes

A permutation of integers from 1 to n is an array of size n where each integer from 1 to n appears exactly once.

You are given a permutation p of integers from 1 to n. You have to process n queries. During the i-th query, you replace pdip_{d_i}pdi with 0. Each element is replaced with 0 exactly once. The changes made in the queries are saved, that is, after the i-th query, all integers pd1,pd2,...,pdip_{d_1},p_{d_2},...,p_{d_i}pd1,pd2,...,pdi are zeroes.

After each query, you have to find the minimum number of operations required to fix the array; in other words, to transform the current array into any permutation of integers from 1 to n (possibly into the original permutation p, possibly into some other permutation).

The operation you can perform to fix the array is the following one:

choose the integer i from 1 to n, replace the i-th element of the array with i.

Note that the answer for each query is calculated independently, meaning you do not actually apply any operations, just calculate the minimum number of operations.

Input

Each test consists of several test cases. The first line contains one integer t (1≤t≤10410^4104) --- the number of test cases. Then the test cases follow.

The first line of each test case contains a single integer n (1≤n≤10510^5105).

The second line of each test case contains n integers p1,p2,...,pnp_1,p_2,...,p_np1,p2,...,pn (1≤pi≤n) --- the original permutation. All pi are distinct.

The third line of each test case contains n integers d1,d2,...,dnd_1,d_2,...,d_nd1,d2,...,dn (1≤di≤n). All di are distinct.

Additional constraint on the input:

the sum of n across all test cases does not exceed 2⋅10510^5105.

Output

For each test case, output a line containing n integers, where the i-th integer should be equal to the minimum number of operations required to fix the array which was obtained after the i-th query (i.e., the permutation p where all integers pd1,pd2,...,pdip_{d_1},p_{d_2},...,p_{d_i}pd1,pd2,...,pdi are replaced by zeroes).

Example

Input

3

3

1 2 3

3 2 1

5

4 5 3 1 2

4 5 1 3 2

7

4 3 1 2 7 5 6

1 2 3 4 5 6 7

Output

1 2 3

2 4 4 5 5

4 4 4 4 7 7 7

Note

In the first test case, after each query, every integer which was replaced by 0

can be restored by one operation.

In the second test case, you can act as follows:
:
p=[4,5,3,0,2], it can be transformed into [1,5,3,4,2].

复制代码
Query 2

:
p=[4,5,3,0,0], it can be transformed into [1,2,3,4,5].

复制代码
Query 3

:
p=[0,5,3,0,0], it can be transformed into [1,2,3,4,5].

复制代码
Query 4

:
p=[0,5,0,0,0], it can be transformed into [1,2,3,4,5].

复制代码
Query 5

:
p=[0,0,0,0,0], it can be transformed into [1,2,3,4,5].

复制代码
The numbers that were changed are highlighted in red.

中文

题目描述

一个从 111 到 nnn 的整数排列是指一个大小为 nnn 的数组,其中每个从 111 到 nnn 的整数恰好出现一次。

给定一个从 111 到 nnn 的排列 ppp。你需要处理 nnn 个查询。在第 iii 次查询中,你将 pdip_{d_i}pdi 替换为 000。每个元素恰好会被替换为 000 一次。查询中的修改会被保留,也就是说,在第 iii 次查询后,所有整数 pd1,pd2,...,pdip_{d_1}, p_{d_2}, \dots, p_{d_i}pd1,pd2,...,pdi 都会变为 000。

在每次查询后,你需要找到修复数组所需的最少操作次数;换句话说,将当前数组转换为从 111 到 nnn 的任意排列(可能是原始排列 ppp,也可能是其他排列)。

修复数组的操作如下:

  • 选择一个从 111 到 nnn 的整数 iii,将数组的第 iii 个元素替换为 iii。

注意,每个查询的答案是独立计算的,这意味着你实际上不会执行任何操作,只是计算最少操作次数。

输入格式

每个测试包含多个测试用例。第一行包含一个整数 ttt(1≤t≤1041 \le t \le 10^{4}1≤t≤104)------测试用例的数量。接下来是测试用例的描述。

每个测试用例的第一行包含一个整数 nnn(1≤n≤1051 \le n \le 10^{5}1≤n≤105)。

第二行包含 nnn 个整数 p1,p2,...,pnp_1, p_2, \dots, p_np1,p2,...,pn(1≤pi≤n1 \le p_{i} \le n1≤pi≤n)------原始排列。所有 pip_ipi 互不相同。

第三行包含 nnn 个整数 d1,d2,...,dnd_1, d_2, \dots, d_nd1,d2,...,dn(1≤di≤n1 \le d_{i} \le n1≤di≤n)。所有 did_{i}di 互不相同。

输入数据的额外限制:

  • 所有测试用例的 nnn 之和不超过 2⋅1052 \cdot 10^{5}2⋅105。

输出格式

对于每个测试用例,输出一行包含 nnn 个整数,其中第 iii 个整数表示在第 iii 次查询后(即排列 ppp 中所有整数 pd1,pd2,...,pdip_{d_1}, p_{d_2}, \dots, p_{d_i}pd1,pd2,...,pdi 被替换为 000 后)修复数组所需的最少操作次数。

输入输出样例 #1

输入 #1

复制代码
3
3
1 2 3
3 2 1
5
4 5 3 1 2
4 5 1 3 2
7
4 3 1 2 7 5 6
1 2 3 4 5 6 7

输出 #1

复制代码
1 2 3 
2 4 4 5 5 
4 4 4 4 7 7 7

说明/提示

  • 在第一个测试用例中,每次查询后,每个被替换为 000 的整数都可以通过一次操作恢复。
  • 在第二个测试用例中,可以按以下方式操作:
    • 查询 111:p=[4,5,3,0,2]p = [4, 5, 3, 0, 2]p=[4,5,3,0,2],可以转换为 [1,5,3,4,2][{\color{red}1}, 5, 3, {\color{red}4}, 2][1,5,3,4,2]。
    • 查询 222:p=[4,5,3,0,0]p = [4, 5, 3, 0, 0]p=[4,5,3,0,0],可以转换为 [1,2,3,4,5][{\color{red}1}, {\color{red}2}, 3, {\color{red}4}, {\color{red}5}][1,2,3,4,5]。
    • 查询 333:p=[0,5,3,0,0]p = [0, 5, 3, 0, 0]p=[0,5,3,0,0],可以转换为 [1,2,3,4,5][{\color{red}1}, {\color{red}2}, 3, {\color{red}4}, {\color{red}5}][1,2,3,4,5]。
    • 查询 444:p=[0,5,0,0,0]p = [0, 5, 0, 0, 0]p=[0,5,0,0,0],可以转换为 [1,2,3,4,5][{\color{red}1}, {\color{red}2}, {\color{red}3}, {\color{red}4}, {\color{red}5}][1,2,3,4,5]。
    • 查询 555:p=[0,0,0,0,0]p = [0, 0, 0, 0, 0]p=[0,0,0,0,0],可以转换为 [1,2,3,4,5][{\color{red}1}, {\color{red}2}, {\color{red}3}, {\color{red}4}, {\color{red}5}][1,2,3,4,5]。

标红的数字表示被修改的元素。

翻译由 DeepSeek V3 完成

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 2;
bool table[maxn];
int has[maxn];
int a[maxn];
int n, t;

int main() {
    ios::sync_with_stdio(false);
    cin >> t; 
    while (t--) {
        cin >> n;
        memset(table, false, sizeof(table));
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            has[a[i]] = i;
        }
        int ans = 0;
        int d;
        for (int i = 1; i <= n; i++) {
            cin >> d;
            while (!table[d]) {
                ans += 1;
                table[d] = true;
                d = has[d];
            }
            cout << ans << " ";
        }
        cout << endl;
    }
    return 0;
}

解答

最后每个元素最后都会回到自己的位置,使用一个哈希表记录每个元素的下标,计算每个元素放回到自己的位置的次数即可

相关推荐
小O的算法实验室1 分钟前
2026年IEEE TASE,面对突发危险区域的基于强化学习的多无人机路径规划,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
橘子hhh2 分钟前
Netty基础服务器实现
java·nio
墨雪遗痕4 分钟前
工程架构认知(二):从 CDN 到 Keep-Alive,理解流量如何被“消化”在系统之外
java·spring·架构
AI科技星5 分钟前
全维度相对论推导、光速螺旋时空与北斗 GEO 钟差的统一理论
开发语言·线性代数·算法·机器学习·数学建模
ECT-OS-JiuHuaShan9 分钟前
科学的本来意义,是基于规范的共识逻辑,而非共识方法
人工智能·科技·学习·算法·生活
木子墨51615 分钟前
LeetCode 热题 100 精讲 | 动态规划进阶篇:最大子数组和 · 分割等和子集 · 最长公共子序列 · 打家劫舍 III
数据结构·c++·算法·leetcode·动态规划·力扣
用户66885998476619 分钟前
Sprint Boot登录案例
java
li16709027020 分钟前
第十章:list
c语言·开发语言·数据结构·c++·算法·list·visual studio
Z1Jxxx27 分钟前
C++ P1150 Peter 的烟
数据结构·c++·算法
踮起脚看烟花35 分钟前
chapter10_泛型算法
c++·算法