Triple Sort
Time limit1sMemory limit1024 MB
Given a permutation of 1 to N, decide whether repeatedly reversing any three consecutive elements can sort it into increasing order.
- Level
Medium6 of 10
- Topics
- Math, Combinatorics, Greedy, Implementation
- Solved
- No attempts yet
Problem
After taking an algorithms class, Yoon was so impressed that he decided to invent his own sorting algorithm. His algorithm, "Triple Sort," works as follows.
- Pick any three elements at consecutive positions in the array.
- Reverse the order of the three elements. For example, if the three elements are in that order, after reversing they become .
- Repeat the process until the array is sorted in increasing order.
Yoon then realized that Triple Sort cannot sort every array, and he was disappointed. Given an array containing each integer from to exactly once, write a program to determine whether Triple Sort can sort it.
Input
The first line gives an integer , the size of the array.
The second line gives the elements of the array in order, separated by spaces.
Output
Print YES if Triple Sort can sort the given array in increasing order, and NO otherwise.
Constraints
Each integer from to appears exactly once in the given array.