1. 

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 120 - Stacks of Flapjacks
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=56
*
* @author Evgeni Pavlidis
* @version 1.0, 06/02/2010
*
* Method : Simulation
* Status : Accepted
* Runtime: 0.564
*/

import java.io.*;
import java.util.*;

class Main {

public static void main(String...args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input, output;
String inputs[];
int n;
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> solution = new Stack<Integer>();

while( (input = reader.readLine()) != null)
{
inputs = input.split(" ");
n = inputs.length;
int[] a = new int[n];

for(int i = 0; i < n; i++)
a[n-i-1] = Integer.parseInt(inputs[i]);

System.out.println(input);

int max, next = 0;
for(int i = 0; i < n; i++)
{
next = i;
stack.clear();

// find max
for(int j = i; j < n; j++)
if(a[j] > a[next])
next = j;

//System.out.println("==> " + Arrays.toString(a));
//System.out.println("==> " + (i+1) + " " + (next+1));

if(next == i)
continue;

// flip
if(next < n-1)
{
for(int j = next; j < n; j++)
stack.push(a[j]);

for(int j = next; j < n; j++)
a[j] = stack.pop();

System.out.print((next+1) + " ");
}

for(int j = i; j < n; j++)
stack.push(a[j]);

for(int j = i; j < n; j++)
a[j] = stack.pop();

System.out.print((i+1) + " ");
}

System.out.println(0);
}
}
}