## DMOPC '18 Contest 1 P2 - Sorting

View as PDF

Points: 5 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

You are given a natural number and a permutation of the numbers . Since is a permutation, it has length and each number from to appears exactly once. You are allowed to swap two elements if their positions are exactly apart. In other words, you may swap and only if . Determine if can be sorted from least to greatest.

#### Input Specification

The first line will contain two space-separated integers, and .
The next line will contain space-separated integers, .

#### Output Specification

Output YES if it may be sorted and NO otherwise.

#### Sample Input 1

5 1
1 4 3 2 5

#### Sample Output 1

YES

#### Explanation for Sample 1

Swap and . The sequence is now 1 4 2 3 5.
Swap and . The sequence is now 1 2 4 3 5.
Swap and . The sequence is now 1 2 3 4 5, which is sorted.

#### Sample Input 2

5 2
5 4 3 2 1

#### Sample Output 2

YES

#### Explanation for Sample 2

Swap and . The sequence is now 5 2 3 4 1.
Swap and . The sequence is now 3 2 5 4 1.
Swap and . The sequence is now 3 2 1 4 5.
Swap and . The sequence is now 1 2 3 4 5, which is sorted.

#### Sample Input 3

5 3
5 4 3 2 1

#### Sample Output 3

NO