首页 > 其他分享 >C. MEX Repetition

C. MEX Repetition

时间:2023-08-31 14:55:08浏览次数:104  
标签:10 3mu ldots mkern Repetition MEX mkern3mu

C. MEX Repetition

You are given an array $a_1,a_2,\ldots, a_n$ of pairwise distinct integers from $0$ to $n$. Consider the following operation:

  • consecutively for each $i$ from $1$ to $n$ in this order, replace $a_i$ with $\operatorname{MEX}(a_1, a_2, \ldots, a_n)$.

Here $\operatorname{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_m$ is defined as the smallest non-negative integer $x$ which does not occur in the collection $c$. For example, $\operatorname{MEX}(0, 2, 2, 1, 4) = 3$ and $\operatorname{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$ ($1 \le t \le 10^5$). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $k$ ($1\le n\le 10^5$, $1\le k\le 10^9$).

The second line contains $n$ pairwise distinct integers $a_1,a_2,\ldots, a_n$ ($0\le a_i\le n$) 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 $10^5$.

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 $\operatorname{MEX}(1) = 0$.
  2. On the second operation, the array changes from $[0]$ to $[1]$, since $\operatorname{MEX}(0) = 1$.

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

In the second test case, the array changes as follows during one operation: $[{\mkern3mu\underline{\mkern-3mu {\bf 0}\mkern-3mu}\mkern3mu}, 1, 3] \rightarrow [2, {\mkern3mu\underline{\mkern-3mu {\bf 1}\mkern-3mu}\mkern3mu}, 3] \rightarrow [2, 0, {\mkern3mu\underline{\mkern-3mu {\bf 3}\mkern-3mu}\mkern3mu}] \rightarrow [2, 0, 1]$.

In the third test case, the array changes as follows during one operation: $[{\mkern3mu\underline{\mkern-3mu {\bf 0}\mkern-3mu}\mkern3mu}, 2] \rightarrow [1, {\mkern3mu\underline{\mkern-3mu {\bf 2}\mkern-3mu}\mkern3mu}] \rightarrow [1, 0]$. And during the second operation: $[{\mkern3mu\underline{\mkern-3mu {\bf 1}\mkern-3mu}\mkern3mu}, 0] \rightarrow [2, {\mkern3mu\underline{\mkern-3mu {\bf 0}\mkern-3mu}\mkern3mu}] \rightarrow [2, 1]$.

 

解题思路

  看到$k$这么大肯定是有循环节的。在原有数组$a$的情况下令$a_{n+1} = \operatorname{MEX}(a_1, \ldots, a_n)$,此时长度为$n+1$的数组$a$就是$0 \sim n$的一个排列。每轮中的$a_i = \operatorname{MEX}(a_1, \ldots, a_n)$其实就是交换$a_i$和$a_{n+1}$的值,因为在交换后还是$0 \sim n$的一个排列,因此$\operatorname{MEX}(a_1, \ldots, a_n)$还是等于$a_{n+1}$。

  所以在一轮操作之后,数组从$[a_1, a_2, \ldots, a_{n+1}]$变到$[a_{n+1}, a_1, a_2, \ldots, a_n]$,本质就是循环右移一个单位。所以最终答案就是将原数组补充$a_{n+1} = \operatorname{MEX}(a_1, \ldots, a_n)$后循环右移$k \bmod (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

标签:10,3mu,ldots,mkern,Repetition,MEX,mkern3mu
From: https://www.cnblogs.com/onlyblues/p/17669544.html

相关文章

  • softmax,logsumexp, softmax的上溢(overflow)或下溢
    LSE:logsumexp   ......
  • SystemExit异常 sys.exit()​​函数
    这个错误是SystemExit,它是Python中的一个异常类。当调用sys.exit()函数时,会引发SystemExit异常,这个函数用于退出程序。在你的代码中,sys.exit(0)被调用,参数0表示正常退出程序。在这种情况下,fun_read()函数中的某个条件被满足,导致程序调用了sys.exit(0)来退出。这可能是为了在某些情......
  • Atcoder AGC062C Mex of Subset Sum
    对\(a_i\)从小到大进行排序,因为想到若\(<a_{i-1}\)的不能取到的数因为\(a_i>a_{i-1}\)肯定是能保证取不到的。对排完序的\(a_i\)做一个前缀和\(s_i=\sum\limits_{j=1}^n\),令\(A_i\)为\(a_{1\simi}\)中无法表示为子序列之和且\(<s_i\)的正整数的集合......
  • Codeforces 1740H - MEX Tree Manipulation
    首先发现一个性质,那就是每个点的点权是\(\logn\)级别的。因为假设要造出一个点权为\(i\)的点至少需要大小为\(mn_i\)的子树,那么显然有\(mn_i=\sum\limits_{j=0}^{i-1}mn_j+1\),即\(mn_i=2^i\)。由于点权不是很大,因此我们很容易地往变换复合的角度思考。将整棵树进行轻重......
  • AT_agc062_c [AGC062C] Mex of Subset Sum 思维妙妙题--zhengjun
    思路比较巧妙。首先排序。考虑目前维护出\(a_{1\simi}\)不能表示的数的集合\(S\)。考虑如何加入\(a_{i+1}\)。如果当前\(<a_i\)的数足够了,直接输出(因为这些数将会一直留在\(S\)中)。记\(sum=\sum\limits_{j=1}^{i}a_j\)。\(a_{i+1}>sum\)\[S'=S\cup[sum+1,a_{i+......
  • 【atcoder beginner 308E - MEX】
    前缀和二分查找打表枚举代码如下importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;importjava.util.ArrayList;importjava.util.List;publicclassMain{publicstaticIntege......
  • chemex访问首页提示404 Not Found
           问题描述:由于windows下用phpstudy集成环境部署,中途调试其它项目时,把apache切换成nginx,再次切换回apache时,chemex站点的伪静态配置变成空白了,导致chemex首页访问时提示404 问题原因:nginx或apache服务器未配置伪静态。 解决方法:如果是nginx服务器......
  • Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,6] Me
     报错如下:Causedby:javax.xml.stream.XMLStreamException:ParseErrorat[row,col]:[2,6]Message:不允许有匹配"[xX][mM][lL]"的处理指令目标。原因:xml第一行为空行,所以报错 需要将<?xmlversion="1.0"encoding="utf-8"?>放在第一行即可解决问题  ......
  • org.springframework.data.redis.RedisSystemException: Redis exception; nested exc
    springBoot+redis.程序隔一段时间会莫名其妙的报Redis的错误.报错如下:org.springframework.data.redis.RedisSystemException:Redisexception;nestedexceptionisio.lettuce.core.RedisException:java.io.IOException:Connectionresetbypeer百度得知说:是因为re......
  • 区间 mex 问题
    可以考虑以下P2709的做法。先用莫队取下出现在\([l_i,r_i]\)的位置的数,然后二分求得\(ask(x)=x\)的最大\(x\)就是答案。注意\(0\)不能加入树状数组,于是先给所有数加\(1\)。块长取\(n^{0.55}\)最佳。#include<bits/stdc++.h>usingnamespacestd;constintN=......