8012: Sleepy Cow Sorting

内存限制:128 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:29 通过:14

题目描述

Farmer John正在尝试将他的N头奶牛(1≤N≤105),方便起见编号为1…N,在她们前往牧草地吃早餐之前排好顺序。

当前,这些奶牛以p1,p2,p3,…,pN的顺序排成一行,Farmer John站在奶牛p1前面。他想要重新排列这些奶牛,使得她们的顺序变为1,2,3,…,N,奶牛1在Farmer John旁边。

今天奶牛们有些困倦,所以任何时刻都只有直接面向Farmer John的奶牛会注意听Farmer John的指令。每一次他可以命令这头奶牛沿着队伍向后移动k步,k可以是1到N−1之间的任意数。她经过的k头奶牛会向前移动,腾出空间使得她能够插入到队伍中这些奶牛之后的位置。

例如,假设N=4,奶牛们开始时是这样的顺序:

 FJ: 4, 3, 2, 1 

唯一注意FJ指令的奶牛是奶牛4。当他命令她向队伍后移动2步之后,队伍的顺序会变成:

 FJ: 3, 2, 4, 1 

现在唯一注意FJ指令的奶牛是奶牛3,所以第二次他可以给奶牛3下命令,如此进行直到奶牛们排好了顺序。

Farmer John急欲完成排序,这样他就可以回到他的农舍里享用他自己的早餐了。请帮助他求出一个操作序列,使得能够用最少的操作次数将奶牛们排好顺序。

输入格式(文件名:sleepy.in):

输入的第一行包含N。第二行包含N个空格分隔的整数:p1,p2,p3,…,pN,表示奶牛们的起始顺序。

输出格式(文件名:sleepy.out):

输出的第一行包含一个整数,K,为将奶牛们排好顺序所需的最小操作次数。 第二行包含K个空格分隔的整数,c1,c2,…,cK,每个数均在1…N−1之间。如果第i次操作FJ命令面向他的奶牛向队伍后移动ci步,那么K次操作过后奶牛们应该排好了顺序。
如果存在多种最优的操作序列,你的程序可以输出其中任何一种。

输入样例:

4
1 2 4 3

输出样例:

3
2 2 3


Farmer John is attempting to sort his N cows (1≤N≤105), conveniently numbered 1…N, before they head out to the pastures for breakfast.

Currently, the cows are standing in a line in the order p1,p2,p3,…,pN, and Farmer John is standing in front of cow p1. He wants to reorder the cows so that they are in the order 1,2,3,…,N, with cow 1 next to Farmer John.

Today the cows are a bit sleepy, so at any point in time the only cow who is paying attention to Farmer John's instructions is the cow directly facing Farmer John. In one time step, he can instruct this cow to move k paces down the line, for any k between 1 and N−1 inclusive. The k cows whom she passes will amble forward, making room for her to insert herself in the line after them.

For example, suppose that N=4 and the cows start off in the following order:

 FJ: 4, 3, 2, 1 

The only cow paying attention to FJ is cow 4. If he instructs her to move 2 paces down the line, the order will subsequently look like this:

 FJ: 3, 2, 4, 1 

Now the only cow paying attention to FJ is cow 3, so in the second time step he may give cow 3 an instruction, and so forth until the cows are sorted.

Farmer John is eager to complete the sorting, so he can go back to the farmhouse for his own breakfast. Help him find a sequence of instructions that sorts the cows in the minimum number of time steps.

INPUT FORMAT (file sleepy.in):

The first line of input contains N. The second line contains N space-separated integers: p1,p2,p3,…,pN, indicating the starting order of the cows.

OUTPUT FORMAT (file sleepy.out):

The first line should contain a single integer, K, giving the minimum number of time steps required to sort the cows. The second line should contain K space-separated integers, c1,c2,…,cK, each in the range 1…N−1. Furthermore, if in the i-th time step FJ instructs the cow facing him to move ci paces down the line, then after K time steps the cows should be in sorted order.
If there are multiple optimal instruction sequences, your program may output any of them.

SAMPLE INPUT:

4
1 2 4 3

SAMPLE OUTPUT:

3
2 2 3

输入样例 复制

4
1 2 4 3

输出样例 复制

3
2 2 3