PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000201. Sắp xếp đàn bò buồn ngủ (Sleepy Cow Sorting)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn cows numbered 11 through nn, each appearing exactly once. They stand in a line p1,p2,…,pnp_1,p_2,\ldots,p_n, from the cow closest to Farmer John to the back. He wants the line ordered as 1,2,…,n1,2,\ldots,n.

The cows are sleepy, so at each step only the first cow follows his instruction. Farmer John chooses an integer cc with 1≤c≤n−11\le c\le n-1 and moves the first cow past exactly the next cc cows, inserting her immediately behind those cc 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 nn.
  • The second line contains exactly nn integers p1,p2,…,pnp_1,p_2,\ldots,p_n in line order. The array pp is a permutation of 1,2,…,n1,2,\ldots,n.

Output

  • Print an integer KK on the first line: the minimum number of operations.
  • On the second line print KK integers c1,c2,…,cKc_1,c_2,\ldots,c_K in execution order. The integer cic_i is the number of cows passed by the front cow at step ii.

If K=0K=0, there are no instructions to print on the second line.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; (p1,…,pn)(p_1,\ldots,p_n) is a permutation of 1,…,n1,\ldots,n; each instruction is in [1,n−1][1,n-1] whenever an operation exists.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; (p1,…,pn)(p_1,\ldots,p_n) is a permutation of 1,…,n1,\ldots,n; each instruction is in [1,n−1][1,n-1] whenever an operation exists.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le 10^5; (p1,…,pn)(p_1,\ldots,p_n) is a permutation of 1,…,n1,\ldots,n; each instruction is in [1,n−1][1,n-1] whenever an operation exists.

Examples

Input

4
1 2 4 3

Output

3
2 2 3

Explanation

The initial line is [1,2,4,3][1,2,4,3]. Execute the three instructions 2,2,32,2,3:

  1. Cow 11 moves past cows 22 and 44, giving [2,4,1,3][2,4,1,3].
  2. Cow 22 moves past cows 44 and 11, giving [4,1,2,3][4,1,2,3].
  3. Cow 44 moves past cows 1,2,31,2,3, giving [1,2,3,4][1,2,3,4].

Initially, only the final suffix [3][3] 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.