原题
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;
}
解答
最后每个元素最后都会回到自己的位置,使用一个哈希表记录每个元素的下标,计算每个元素放回到自己的位置的次数即可