3-Second Sort
InterviewTime limit2sMemory limit1024 MB
Given a sequence that is not sorted, decide whether at most 3 element replacements can make it nondecreasing, and output one such sequence of operations.
- Level
Medium7 of 10
- Topics
- Greedy, Implementation, Array, Sorting
- Solved
- No attempts yet
Problem
While you are peacefully reading this problem, the extremist fundamentalist mint-chocolate terrorist Kim Junwon has already seized the school outside the auditorium.
If you do not make the given sequence sorted in ascending order within 3 seconds, the mint-chocolate bomb planted in the auditorium will explode.
You may replace any element of the sequence with another number .
This operation takes 1 second.
3...
2...
1...
Input
The first line gives the length of the sequence you must make sorted.
The second line gives integers representing the elements of the sequence, separated by spaces.
Output
If you can make the sequence sorted in ascending order within 3 operations,
-
Print
YESon the first line. -
From the second line onward, print a method to make the sequence sorted in the following form.
- On the first line, print the number of operations .
- Then, over lines, print the operations to apply in order.
Specifically, on each line print two integers and . This means the operation of replacing element with . (, )
You do not need to minimize the number of operations, and if several methods are possible, you may print any one of them. You may modify the same element multiple times.
If you cannot make the sequence sorted in ascending order within 3 operations,
- Print
NOon the first line.
Constraints
- ()
- The sequence is not given already sorted in ascending order.
Hint
A sequence being sorted in ascending order means that , , , .