#P6795. [SNOI2020] 排列
[SNOI2020] 排列
Description
There is a permutation of length . The first positions have already been fixed.
In the permutation , define that is a value-contiguous segment if and only if:
$$\max(p_l, p_{l+1}, \dots, p_r) - \min(p_l, p_{l+1}, \dots, p_r) = r-l$$The number of value-contiguous segments in is the total count of such segments among all .
You need to find, among all possible permutations , the maximum possible number of value-contiguous segments, and output any one permutation that achieves it.
Input Format
The first line contains two integers , representing the length of the permutation and the number of fixed positions.
The next line contains positive integers separated by spaces, representing the fixed part of the permutation. (If , this line is empty.)
Output Format
Output the first line with one integer, the maximum number of value-contiguous segments.
Output the second line with positive integers, representing any one valid permutation.
4 1
2
8
2 1 3 4
Hint
Sample Explanation
For sample , an optimal solution is , which has value-contiguous segments (). is another optimal solution.
Constraints
For all testdata, .
- For of the testdata, ;
- For another of the testdata, ;
- For another of the testdata, ;
- For another of the testdata, ;
- For the remaining of the testdata, there are no special restrictions.
Translated by ChatGPT 5
京公网安备 11011102002149号