C. MEX Repetition

C. MEX Repetition

You are given an array a1,a2,,an of pairwise distinct integers from 0 to n. Consider the following operation:

  • consecutively for each i from 1 to n in this order, replace ai with MEX(a1,a2,,an).

Here MEX of a collection of integers c1,c2,,cm is defined as the smallest non-negative integer x which does not occur in the collection c. For example, MEX(0,2,2,1,4)=3 and MEX(1,2)=0.

Print the array after applying k such operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t105). The description of the test cases follows.

The first line of each test case contains two integers n and k (1n105, 1k109).

The second line contains n pairwise distinct integers a1,a2,,an (0ain) representing the elements of the array before applying the operations.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output

For each test case, print all n elements of the array after applying k 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:

  1. On the first operation, the array changes from [1] to [0], since MEX(1)=0.
  2. On the second operation, the array changes from [0] to [1], since MEX(0)=1.

Thus, the array becomes [1] after two operations.

In the second test case, the array changes as follows during one operation: [0_,1,3][2,1_,3][2,0,3_][2,0,1].

In the third test case, the array changes as follows during one operation: [0_,2][1,2_][1,0]. And during the second operation: [1_,0][2,0_][2,1].

 

解题思路

  看到k这么大肯定是有循环节的。在原有数组a的情况下令an+1=MEX(a1,,an),此时长度为n+1的数组a就是0n的一个排列。每轮中的ai=MEX(a1,,an)其实就是交换aian+1的值,因为在交换后还是0n的一个排列,因此MEX(a1,,an)还是等于an+1

  所以在一轮操作之后,数组从[a1,a2,,an+1]变到[an+1,a1,a2,,an],本质就是循环右移一个单位。所以最终答案就是将原数组补充an+1=MEX(a1,,an)后循环右移kmod(n+1)个单位。

  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

posted @   onlyblues  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-08-31 石子合并
Web Analytics
点击右上角即可分享
微信分享提示