#FT0000201. Sắp xếp đàn bò buồn ngủ (Sleepy Cow Sorting)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp đàn bò buồn ngủ (Sleepy Cow Sorting)
Sleepy Cow Sorting
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
Farmer John has cows numbered through , each appearing exactly once. They stand in a line , from the cow closest to Farmer John to the back. He wants the line ordered as .
The cows are sleepy, so at each step only the first cow follows his instruction. Farmer John chooses an integer with and moves the first cow past exactly the next cows, inserting her immediately behind those cows. The cows passed move one position forward; the relative order of the other cows remains unchanged.
Find the minimum number of steps needed to sort the line, and output instructions achieving that minimum. If several optimal instruction sequences exist, any one is acceptable.
Input
- The first line contains the number of cows .
- The second line contains exactly integers in line order. The array is a permutation of .
Output
- Print an integer on the first line: the minimum number of operations.
- On the second line print integers in execution order. The integer is the number of cows passed by the front cow at step .
If , there are no instructions to print on the second line.
Subtasks
- Subtask 1 (20 points): ; is a permutation of ; each instruction is in whenever an operation exists.
- Subtask 2 (30 points): ; is a permutation of ; each instruction is in whenever an operation exists.
- Subtask 3 (50 points): ; is a permutation of ; each instruction is in whenever an operation exists.
Examples
Input
4
1 2 4 3
Output
3
2 2 3
Explanation
The initial line is . Execute the three instructions :
- Cow moves past cows and , giving .
- Cow moves past cows and , giving .
- Cow moves past cows , giving .
Initially, only the final suffix is strictly increasing. Each step moves the front cow and preserves the relative order of all others. Thus the first three cows must all move, requiring at least three steps. The displayed instructions attain this lower bound, so the output is 3 followed by 2 2 3.