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

解答

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

相关推荐
二年级程序员3 小时前
C 语言文件操作
c语言
爱敲代码的TOM3 小时前
基础算法技巧总结2(算法技巧零碎点,基础数据结构,数论模板)
数据结构·算法
小莞尔3 小时前
【51单片机】【protues仿真】 基于51单片机波形发生器系统
c语言·单片机·嵌入式硬件·物联网·51单片机
cyforkk3 小时前
15、Java 基础硬核复习:File类与IO流的核心逻辑与面试考点
java·开发语言·面试
李少兄3 小时前
解决 org.springframework.context.annotation.ConflictingBeanDefinitionException 报错
java·spring boot·mybatis
大飞哥~BigFei3 小时前
整数ID与短字符串互转思路及开源实现分享
java·开源
benjiangliu3 小时前
LINUX系统-09-程序地址空间
android·java·linux
liu_endong3 小时前
杰发科技AC7840——打印所有GPIO的PORT配置寄存器
mcu·算法·杰发科技·autochips·车规芯片
老鼠只爱大米3 小时前
LeetCode经典算法面试题 #199:二叉树的右视图(BFS双队列法、DFS递归法等多种实现方案详细解析)
算法·leetcode·二叉树·dfs·bfs·深度优先搜索·右视图
历程里程碑3 小时前
子串-----和为 K 的子数组
java·数据结构·c++·python·算法·leetcode·tornado