#GD0000211. Sắp xếp bằng đảo dãy con (Reverse Sort)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp bằng đảo dãy con (Reverse Sort)
Reverse Sort
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Ashish has a binary string of length and wants to sort it in non-decreasing order, with all 0 digits before all 1 digits.
In one operation, select a subsequence of positions, not necessarily consecutive, . Before the operation, the digits at those positions must form a non-increasing sequence:
Reverse the selected digits and write them back into the same positions; all unselected positions remain unchanged. For example, selected digits 1100 become 0011 after reversal.
Find the minimum number of operations needed to sort the string, and output operations achieving it. If the input is already sorted, no operation is needed.
Input
- The first line contains an integer , the string length.
- The second line contains a binary string of exactly characters.
Output
- The first line contains the integer , the minimum number of operations needed.
- Print exactly further lines. Each line contains an integer followed by exactly strictly increasing indices numbered from . At the moment of that operation, selected digits must be non-increasing.
If , print only 0. Any shortest valid sequence of operations is accepted.
Subtasks
- Subtask 1 (20 points): ; ; operation indices are integers from through .
- Subtask 2 (30 points): ; ; operation indices are integers from through .
- Subtask 3 (50 points): ; ; operation indices are integers from through .
Examples
Input
5
10100
Output
1
4 1 3 4 5
Explanation
The input 10100 contains three zeros and two ones, so the sorted target is 00011. The original differs from the target, hence zero operations cannot suffice. Choose indices 1, 3, 4, 5; their current digits are 1100, which are non-increasing. Reversing them yields 0011, changing the entire string into 00011. Thus the minimum is one operation, and 4 1 3 4 5 is a valid operation line.