#K55662. Permutation Commands

    ID: 30026 Type: Default 1000ms 256MiB

Permutation Commands

Permutation Commands

You are given (N) animals initially arranged in ascending order ( [1, 2, \dots, N] ) and a target permutation. You also have (M) commands available. Each command is considered a swap operation. In the worst-case scenario, any permutation can be achieved in (N-1) swaps. Thus, if (M \geq N-1), you can always reach the target permutation. Otherwise, if (M < N-1), the target permutation can only be achieved if it is already sorted in ascending order.

For example:

  • For \(N = 4\), \(M = 3\) with target permutation [2, 1, 4, 3], the answer is YES because \(M \geq N-1\).
  • For \(N = 5\), \(M = 1\) with target permutation [5, 1, 2, 3, 4], the answer is NO.
Determine whether the target permutation can be achieved under the given constraints.

inputFormat

The first line of input contains two integers (N) and (M) representing the number of animals and the number of commands, respectively. The second line contains (N) space-separated integers denoting the target permutation.

outputFormat

Output a single line containing YES if it is possible to achieve the target permutation with at most (M) commands, or NO otherwise.## sample

4 3
2 1 4 3
YES