1294D.md (3866B)
1 Source: https://codeforces.com/problemset/problem/1294/D 2 3 D. MEX maximizing 4 5 time limit per test 6 7 3 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: 22 23 * for the array $[0, 0, 1, 0, 2]$ MEX equals to $3$ because numbers $0, 1$ and $2$ are presented in the array and $3$ is the minimum non-negative integer not presented in the array; 24 * for the array $[1, 2, 3, 4]$ MEX equals to $0$ because $0$ is the minimum non-negative integer not presented in the array; 25 * for the array $[0, 1, 4, 3]$ MEX equals to $2$ because $2$ is the minimum non-negative integer not presented in the array. 26 27 You are given an empty array $a=[]$ (in other words, a zero-length array). You are also given a positive integer $x$. 28 29 You are also given $q$ queries. The $j$-th query consists of one integer $y_j$ and means that you have to append one element $y_j$ to the array. The array length increases by $1$ after a query. 30 31 In one move, you can choose any index $i$ and set $a_i := a_i + x$ or $a_i := a_i - x$ (i.e. increase or decrease any element of the array by $x$). The only restriction is that $a_i$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query. 32 33 You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element). 34 35 You have to find the answer after each of $q$ queries (i.e. the $j$-th answer corresponds to the array of length $j$). 36 37 Operations are discarded before each query. I.e. the array $a$ after the $j$-th query equals to $[y_1, y_2, \dots, y_j]$. 38 39 Input 40 41 The first line of the input contains two integers $q, x$ ($1 \le q, x \le 4 \cdot 10^5$) — the number of queries and the value of $x$. 42 43 The next $q$ lines describe queries. The $j$-th query consists of one integer $y_j$ ($0 \le y_j \le 10^9$) and means that you have to append one element $y_j$ to the array. 44 45 Output 46 47 Print the answer to the initial problem after each query — for the query $j$ print the maximum value of MEX after first $j$ queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries. 48 49 Examples 50 51 Input 52 53 ``` 54 7 3 55 0 56 1 57 2 58 2 59 0 60 0 61 10 62 ``` 63 64 Output 65 66 ``` 67 1 68 2 69 3 70 3 71 4 72 4 73 7 74 ``` 75 76 Input 77 78 ``` 79 4 3 80 1 81 2 82 1 83 2 84 ``` 85 86 Output 87 88 ``` 89 0 90 0 91 0 92 0 93 ``` 94 95 Note 96 97 In the first example: 98 99 * After the first query, the array is $a=[0]$: you don't need to perform any operations, maximum possible MEX is $1$. 100 * After the second query, the array is $a=[0, 1]$: you don't need to perform any operations, maximum possible MEX is $2$. 101 * After the third query, the array is $a=[0, 1, 2]$: you don't need to perform any operations, maximum possible MEX is $3$. 102 * After the fourth query, the array is $a=[0, 1, 2, 2]$: you don't need to perform any operations, maximum possible MEX is $3$ (you can't make it greater with operations). 103 * After the fifth query, the array is $a=[0, 1, 2, 2, 0]$: you can perform $a[4] := a[4] + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3]$. Now MEX is maximum possible and equals to $4$. 104 * After the sixth query, the array is $a=[0, 1, 2, 2, 0, 0]$: you can perform $a[4] := a[4] + 3 = 0 + 3 = 3$. The array changes to be $a=[0, 1, 2, 2, 3, 0]$. Now MEX is maximum possible and equals to $4$. 105 * After the seventh query, the array is $a=[0, 1, 2, 2, 0, 0, 10]$. You can perform the following operations: 106 + $a[3] := a[3] + 3 = 2 + 3 = 5$, 107 + $a[4] := a[4] + 3 = 0 + 3 = 3$, 108 + $a[5] := a[5] + 3 = 0 + 3 = 3$, 109 + $a[5] := a[5] + 3 = 3 + 3 = 6$, 110 + $a[6] := a[6] - 3 = 10 - 3 = 7$, 111 + $a[6] := a[6] - 3 = 7 - 3 = 4$.The resulting array will be $a=[0, 1, 2, 5, 3, 6, 4]$. Now MEX is maximum possible and equals to $7$.