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

解答

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

相关推荐
祈祷苍天赐我java之术5 小时前
Linux 进阶之性能调优,文件管理,网络安全
java·linux·运维
无处不在的海贼5 小时前
小明的Java面试奇遇之发票系统相关深度实战挑战
java·经验分享·面试
武子康5 小时前
Java-109 深入浅出 MySQL MHA主从故障切换机制详解 高可用终极方案
java·数据库·后端·mysql·性能优化·架构·系统架构
秋难降5 小时前
代码界的 “建筑师”:建造者模式,让复杂对象构建井然有序
java·后端·设计模式
bdgtd881785 小时前
动态修补C扩展模块的函数指针有哪些风险?安全的修补方案是什么?
c语言·开发语言·安全
mit6.8246 小时前
8.27 网格memo
c++·算法
jeffery8926 小时前
4056:【GESP2403八级】接竹竿
数据结构·c++·算法
BillKu6 小时前
Spring Boot 多环境配置
java·spring boot·后端
Ghost-Face6 小时前
图论基础
算法
默归6 小时前
分治法——二分答案
python·算法