#P6795. Maximizing Contiguous Value Segments

    ID: 20002 Type: Default 1000ms 256MiB

Maximizing Contiguous Value Segments

Maximizing Contiguous Value Segments

You are given an integer n and an integer k along with a fixed prefix of a permutation p of length n defined by p1, p2, \dots, pk (all values are distinct and in the range 1 to n). You need to complete the permutation by choosing the remaining n - k numbers (in any order) such that the total number of contiguous segments (subarrays) that are value‐continuous is maximized.

A contiguous segment \([l,r]\) (where \(1 \le l \le r \le n\)) is called value‐continuous if and only if \[ \max(p_l, p_{l+1}, \dots, p_r) - \min(p_l, p_{l+1}, \dots, p_r) = r - l. \] In other words, the numbers in \(p_l, p_{l+1}, \dots, p_r\) form a set of consecutive integers (although not necessarily sorted).

Your task is to compute two things:

  1. The maximum possible number of value‐continuous segments over all completions of p.
  2. Output any permutation p (that extends the given fixed prefix) which attains this maximum.

Note: If it is possible to arrange the entire permutation such that every adjacent pair differs by exactly 1, then every contiguous segment is value‐continuous and the total number of such segments is \(\frac{n(n+1)}{2}\), which is the maximum possible.

inputFormat

The first line contains two integers n and k (nk). The second line contains k distinct integers: p1, p2, \dots, pk – the fixed prefix of the permutation.

outputFormat

On the first line, output the maximum number of value‐continuous segments over all completions of p.
On the second line, output a permutation of n numbers (space separated) that extends the fixed prefix and achieves this maximum.

sample

5 2
2 3
11

2 3 4 5 1

</p>