当前位置: 首页 > news >正文

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:

Query 1
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.

中文

题目描述

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

给定一个从 111nnn 的排列 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

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

修复数组的操作如下:

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

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

输入格式

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

每个测试用例的第一行包含一个整数 nnn1≤n≤1051 \le n \le 10^{5}1n105)。
第二行包含 nnn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_np1,p2,,pn1≤pi≤n1 \le p_{i} \le n1pin)——原始排列。所有 pip_ipi 互不相同。
第三行包含 nnn 个整数 d1,d2,…,dnd_1, d_2, \dots, d_nd1,d2,,dn1≤di≤n1 \le d_{i} \le n1din)。所有 did_{i}di 互不相同。

输入数据的额外限制:

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

输出格式

对于每个测试用例,输出一行包含 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 的整数都可以通过一次操作恢复。
  • 在第二个测试用例中,可以按以下方式操作:
    • 查询 111p=[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]
    • 查询 222p=[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]
    • 查询 333p=[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]
    • 查询 444p=[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]
    • 查询 555p=[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 完成

代码

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

解答

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

http://www.dtcms.com/a/313900.html

相关文章:

  • C++八股文——设计模式
  • 分布式版本控制工具Git
  • 微服务配置管理:Spring Cloud Alibaba Nacos 实践
  • Scrapy爬虫集成MongoDB存储
  • 基于单片机空气质量检测/气体检测系统
  • FPGA学习笔记——简单的乒乓缓存(RAM)
  • docker容器命令
  • Dbeaver数据库的安装和使用(保姆级别)
  • 嵌入式硬件篇---OpenMV存储
  • 精品PPT | 企业数字化运营平台总体规划建设方案
  • LeetCode热题100——42. 接雨水
  • AI绘画-Stable Diffusion-WebUI的ControlNet用法
  • 设计模式(一)——抽象工厂模式
  • 蓝河操作系统(BlueOS)内核 (VIVO开源)
  • [spring-cloud: 负载均衡]-源码分析
  • Nginx服务做负载均衡网关
  • Rust ⽣成 .wasm 的极致瘦⾝之道
  • 旧物回收小程序:开启绿色生活新篇章
  • SpringBoot3.x入门到精通系列:3.2 整合 RabbitMQ 详解
  • Ethereum:智能合约开发者的“瑞士军刀”OpenZeppelin
  • 白杨SEO:百度搜索开放平台发布AI计划是什么?MCP网站红利来了?顺带说说其它
  • 剧本杀小程序系统开发:开启沉浸式推理社交新纪元
  • 力扣 hot100 Day65
  • 《Python 实用项目与工具制作指南》 · 前言
  • [自动化Adapt] GUI交互(窗口/元素) | 系统配置 | 非侵入式定制化
  • [特殊字符]️ 整个键盘控制无人机系统框架
  • Qt按键响应
  • 更智能的 RibbonBar Spread.NET 18.2Crack
  • QT:交叉编译mysql驱动库
  • 基于鼠标位置的相机缩放和平移命令的实现(原理+源码)