C. MEX Repetition
C. MEX Repetition
You are given an array of pairwise distinct integers from to . Consider the following operation:
- consecutively for each from to in this order, replace with .
Here of a collection of integers is defined as the smallest non-negative integer which does not occur in the collection . For example, and .
Print the array after applying such operations.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and (, ).
The second line contains pairwise distinct integers () representing the elements of the array before applying the operations.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print all elements of the array after applying operations.
Example
input
5 1 2 1 3 1 0 1 3 2 2 0 2 5 5 1 2 3 4 5 10 100 5 3 0 4 2 1 6 9 10 8
output
1 2 0 1 2 1 2 3 4 5 0 7 5 3 0 4 2 1 6 9 10
Note
In the first test case, here is the entire process:
- On the first operation, the array changes from to , since .
- On the second operation, the array changes from to , since .
Thus, the array becomes after two operations.
In the second test case, the array changes as follows during one operation: .
In the third test case, the array changes as follows during one operation: . And during the second operation: .
解题思路
看到这么大肯定是有循环节的。在原有数组的情况下令,此时长度为的数组就是的一个排列。每轮中的其实就是交换和的值,因为在交换后还是的一个排列,因此还是等于。
所以在一轮操作之后,数组从变到,本质就是循环右移一个单位。所以最终答案就是将原数组补充后循环右移个单位。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 const int N = 1e5 + 10; 7 8 int a[N]; 9 bool vis[N]; 10 11 void solve() { 12 int n, m; 13 scanf("%d %d", &n, &m); 14 memset(vis, 0, n + 10); 15 for (int i = 0; i < n; i++) { 16 scanf("%d", a + i); 17 vis[a[i]] = true; 18 } 19 for (int i = 0; i <= n; i++) { 20 if (!vis[i]) { 21 a[n++] = i; 22 break; 23 } 24 } 25 m %= n; 26 for (int i = 0, j = (n - m) % n; i < n - 1; i++) { 27 printf("%d ", a[j]); 28 if (++j == n) j = 0; 29 } 30 printf("\n"); 31 } 32 33 int main() { 34 int t; 35 scanf("%d", &t); 36 while (t--) { 37 solve(); 38 } 39 40 return 0; 41 }
参考资料
Pinely Round 2 Editorial:https://codeforces.com/blog/entry/119902
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17669544.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-08-31 石子合并