Title Description
Description
The man finally ascended the land of Mars and saw the mysterious Martians. Neither humans nor Martians can understand each other's language, but our scientists have invented a method of digital communication. This is the way of communication, first of all, the Martians told a very large number of human scientists, scientists cracked the meaning of the figure, and then put a very small number on this large number, the results told the Martians, as the human answer.
Martians use a very simple way to represent numbers--breaking fingers. The Martians had only one hand, but there were thousands of fingers on the hand, and the fingers were lined up in a column numbered three-in-one. Any of the two fingers of a Martian can exchange positions at will, and they are counted by this method.
A Martian uses a human hand to demonstrate how to count with a finger. If the five fingers-thumb, forefinger, middle finger, ring finger and pinky are numbered 1,2,3,4 and 5 respectively, when they are arranged in the normal order, forming 5 digits 12345, when you exchange the position of the ring finger and the little finger, will form 5 digits 12354, when you put five fingers in the order completely reversed, Will form 54321, in all the 120 5 digits that can be formed, 12345 the smallest, which represents 1;12354 second small, which represents 2;54321 Max, which represents 120. The following table shows the 6 3-digit numbers that can be formed when there are only 3 fingers and the number they represent:
Three-in-number
123
132
213
231
312
321
The number represented
1
2
3
4
5
6
Now you are fortunate to be the first Earth man to communicate with the Martians. A Martian will let you look at his finger, and the scientist will tell you to add a very small number of them. Your task is to add the numbers that the Martians use their fingers to the numbers scientists tell you, and to change the order of the Martians ' fingers according to the added results. The input data guarantees that this result will not exceed the range that the Martian finger can represent.
Enter a description
Input Description
The input consists of three lines, and the first line has a positive integer N, representing the number of Martian fingers (1 <= N <= 10000). The second line is a positive integer M, which represents the small integer to be added (1 <= M <= 100). The next line is an arrangement of 1 to N of n integers, separated by spaces, to indicate the order in which the Martian fingers are arranged.
Output description
Output Description
The output has only one row, and this line contains n integers, indicating the order in which the changed Martian fingers are arranged. Each of the two adjacent numbers is separated by a space and cannot have extra spaces.
Sample input
Sample Input
5
3
1 2 3) 4 5
Sample output
Sample Output
1 2 4) 5 3
Data range and Tips
Data Size & Hint
"Data Size"
For 30% of data,n<=15;
For 60% of data,n<=50;
For all data,n<=10000;
Exercises
Arithmetical.
Do it every time. 3 things: 1. Find the subsequence ending with the last number (set the starting point =k). 2. Find the last value less than a "k-1" and swap with a "k-1". 3. Flip the sequence of K to N.
var n,m,i,k,j:longint;
A:array[0..10001]of Longint;
Procedure JH (var x,y:longint);
var t:longint;
Begin
T:=x;
X:=y;
y:=t;
End
Begin
READLN (N,M);
For I:=1 to n do read (A[i]);
For I:=1 to M do
Begin
K:=n;
while (k>1) and (A[k]<a[k-1]) do Dec (k);
For J:=n Downto K do
If A[J]>A[K-1] Then
Begin
JH (a[j],a[k-1]);
Break
End
For J:=k to (n+k-1) Div 2 do JH (A[j],a[n+k-j]);
End
For I:=1 to N do write (A[i], ");
End.
2004 Mars people