init-commit

This commit is contained in:
lilinyang 2025-05-23 15:27:15 +08:00
commit 18a552597a
3461 changed files with 1150579 additions and 0 deletions

View file

@ -0,0 +1,216 @@
"""#
### 谜题描述
You're given an array of n integers between 0 and n inclusive.
In one operation, you can choose any element of the array and replace it by the MEX of the elements of the array (which may change after the operation).
For example, if the current array is [0, 2, 2, 1, 4], you can choose the second element and replace it by the MEX of the present elements 3. Array will become [0, 3, 2, 1, 4].
You must make the array non-decreasing, using at most 2n operations.
It can be proven that it is always possible. Please note that you do not have to minimize the number of operations. If there are many solutions, you can print any of them.
An array b[1 n] is non-decreasing if and only if b_1 b_2 b_n.
The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:
* The MEX of [2, 2, 1] is 0, because 0 does not belong to the array.
* The MEX of [3, 1, 0, 1] is 2, because 0 and 1 belong to the array, but 2 does not.
* The MEX of [0, 3, 1, 2] is 4 because 0, 1, 2 and 3 belong to the array, but 4 does not.
It's worth mentioning that the MEX of an array of length n is always between 0 and n inclusive.
Input
The first line contains a single integer t (1 t 200) the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (3 n 1000) length of the array.
The second line of each test case contains n integers a_1, , a_n (0 a_i n) elements of the array. Note that they don't have to be distinct.
It is guaranteed that the sum of n over all test cases doesn't exceed 1000.
Output
For each test case, you must output two lines:
The first line must contain a single integer k (0 k 2n) the number of operations you perform.
The second line must contain k integers x_1, , x_k (1 x_i n), where x_i is the index chosen for the i-th operation.
If there are many solutions, you can find any of them. Please remember that it is not required to minimize k.
Example
Input
5
3
2 2 3
3
2 1 0
7
0 7 3 1 3 7 7
9
2 0 1 1 2 4 4 2 0
9
8 4 7 6 1 2 3 0 5
Output
0
2
3 1
4
2 5 5 4
11
3 8 9 7 8 5 9 6 4 1 2
10
1 8 1 9 5 2 4 6 3 7
Note
In the first test case, the array is already non-decreasing (2 2 3).
Explanation of the second test case (the element modified by each operation is colored in red):
* a = [2, 1, 0] ; the initial MEX is 3.
* a = [2, 1, \color{red}{3}] ; the new MEX is 0.
* a = [\color{red}{0}, 1, 3] ; the new MEX is 2.
* The final array is non-decreasing: 0 1 3.
Explanation of the third test case:
* a = [0, 7, 3, 1, 3, 7, 7] ; the initial MEX is 2.
* a = [0, \color{red}{2}, 3, 1, 3, 7, 7] ; the new MEX is 4.
* a = [0, 2, 3, 1, \color{red}{4}, 7, 7] ; the new MEX is 5.
* a = [0, 2, 3, 1, \color{red}{5}, 7, 7] ; the new MEX is 4.
* a = [0, 2, 3, \color{red}{4}, 5, 7, 7] ; the new MEX is 1.
* The final array is non-decreasing: 0 2 3 4 5 7 7.
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
import sys
import math
import random
def mex(a):
b = set(a)
i = 0
while i in b:
i += 1
return i
def asc(a):
for i in range(1,len(a)):
if a[i] < a[i-1]:
return False
return True
t = int(sys.stdin.readline().strip())
for _ in range(t):
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split(' ')))
x = []
for i in range(2*n):
if asc(a):
break
m = mex(a)
if m == n:
j = -1
for i in range(n):
if a[i] != i:
j = i
break
a[j] = m
x.append(j)
else:
a[m] = m
x.append(m)
print(len(x))
print(\" \".join([str(xi+1) for xi in x]))
```
请完成上述谜题的训练场环境类实现包括所有必要的方法
"""
from bootcamp import Basebootcamp
import random
class Dreplacebymexbootcamp(Basebootcamp):
def __init__(self, n=5):
super().__init__()
self.n = n
def case_generator(self):
n = self.n
a = [random.randint(0, n) for _ in range(n)]
return {'n': n, 'array': a}
@staticmethod
def prompt_func(question_case):
n = question_case['n']
array = question_case['array']
array_str = ' '.join(map(str, array))
prompt = f"You are given an array of {n} integers: {array_str}.\n"
prompt += "In each operation, you can choose any element and replace it with the MEX (minimum excluded value) of the array.\n"
prompt += "The MEX is the smallest non-negative integer not present in the array. For example, the MEX of [2, 2, 1] is 0, and the MEX of [0, 1, 3] is 2.\n"
prompt += "Your goal is to make the array non-decreasing using at most 2n operations.\n"
prompt += "A non-decreasing array satisfies a[0] ≤ a[1] ≤ ... ≤ a[n-1].\n"
prompt += "Please provide the sequence of indices (1-based) of the elements you choose to replace in each step.\n"
prompt += "The answer should be in the format: [answer] index1 index2 ... [answer]\n"
return prompt
@staticmethod
def extract_output(output):
start = output.rfind('[answer]')
if start == -1:
return None
end = output.find('[/answer]', start)
if end == -1:
return None
content = output[start + len('[answer]'):end].strip()
if not content:
return []
try:
indices = list(map(int, content.split()))
except ValueError:
return None
return indices
@classmethod
def _verify_correction(cls, solution, identity):
initial_array = identity['array']
n = identity['n']
a = initial_array.copy()
for idx in solution:
if idx < 1 or idx > n:
return False
pos = idx - 1 # 转换为0-based索引
current_mex = cls.mex(a)
a[pos] = current_mex
# 检查数组是否非递减
for i in range(1, n):
if a[i] < a[i-1]:
return False
return True
@staticmethod
def mex(arr):
s = set(arr)
m = 0
while m in s:
m += 1
return m