#P6795. [SNOI2020] 排列

    ID: 5642 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>贪心2020各省省选Special Judge构造

[SNOI2020] 排列

Description

There is a permutation pp of length nn. The first kk positions p1,p2,,pkp_1, p_2, \cdots, p_k have already been fixed.

In the permutation pp, define that [l,r][l, r] 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 pp is the total count of such segments among all 1lrn1 \le l \le r \le n.

You need to find, among all possible permutations pp, the maximum possible number of value-contiguous segments, and output any one permutation that achieves it.

Input Format

The first line contains two integers n,kn, k, representing the length of the permutation and the number of fixed positions.
The next line contains kk positive integers pip_i separated by spaces, representing the fixed part of the permutation. (If k=0k = 0, 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 nn positive integers, representing any one valid permutation.

4 1
2
8
2 1 3 4

Hint

Sample Explanation

For sample 11, an optimal solution is 2,1,3,42,1,3,4, which has 88 value-contiguous segments ([1],[2],[3],[4],[1,2],[3,4],[1,3],[1,4][1], [2], [3], [4], [1,2], [3,4], [1,3], [1,4]). 2,3,4,12,3,4,1 is another optimal solution.

Constraints

For all testdata, 1n2×105,0kn1 \le n \le 2\times 10^5, 0 \le k \le n.

  • For 10%10\% of the testdata, n10n \le 10;
  • For another 20%20\% of the testdata, n22n \le 22;
  • For another 10%10\% of the testdata, k1k \le 1;
  • For another 20%20\% of the testdata, k=nk = n;
  • For the remaining 40%40\% of the testdata, there are no special restrictions.

Translated by ChatGPT 5