ĄĀŅ
Language:

Remote Training on Programming

Problems On-line status Contests FAQ
For authors:
Register  ||  Login
 
Hello, Guest! Login or register.

889. Bubble

Time Limit: 1 seconds
Memory Limit:32768KB
Points:10
View Problem Statistics Submit Problem added Administrator

Bubble sort is well known to all computer scientists. While studying the complexity of this algorithm, a notable programming theorist Bublikov asked himself how many times an element of the original array is moved. To find the answer, he wrote a program, which used array S to count moves in the array A being sorted. To make things easier, Bublikov used numbers 1 to n (in random order) when filling the array to be sorted. He even remembered to fill array S with zeroes during initialization!

Below is an extract from his Pascal program that sorts array A consisting of n elements and counts the moves for each element.

for i=1 to n-1 do

   for j=1 to n-i do

      if A[j]>A[j+1] then

         begin

            r:=A[j]; A[j]:=A[j+1]; A[j+1]:=r;

            inc(S[A[j]]);  inc(S[A[j+1]]);

         end;

After one of his experiments Bublikov noted that all elements in S are equal
to 2. The scientist wondered if it was possible to find out the original order of elements in array A. It turned out that the answer is ambiguous. For example, for n=7 there are two variants of element order in A: “
3 2 1 6 7 4 5” or “3 4 1 2 7 6 5”.

Write a program that will determine the number of ways to restore the original array if every element in S is equal to 2.

Limitations

<= n <= 100

Input

The input file specifies an integer n, the number of elements in the array being sorted.

Output

The first line of the input file should contain an integer k, the number of distinct original arrays corresponding to the given counter values. Then n space-delimited integers follow (all numbers from the range 1 to n). These integers show one of the possible element orders in the array being sorted.

Sample

Standard input

Standard output

7

2

3 2 1 6 7 4 5

5

0

 


View Problem Statistics Submit Author/source:
Sorted Problems / Dynamic programming, recurrent relations /
298. At Shop 889. 843. Domenojgi 68. Equation with Missing Digits 14. Expression
Problems from Contests / ACM Contests / Rybinsk-2010 /
888. C - Numbers 889. 890. E - Maze 891. F - Tree packing 892. G - Solar eclipse
We can all benefit by doing occasional "toy" programs, when artificial restrictions are set up, so that we are forced to push our abilities to the limit. The art of tackling miniproblems with all our energy will sharpen our talents for the real problems. Donald E. Knuth.
time generating 0.078 sec.
© Copyright VSTU, AVT, Nosov D.A.