Sequential table Application 4: Inverse algorithm of element position interchangeTime limit:10 Ms Memory limit:570 KiB Problem description A sequential table of length Len (1<=len<=1000000), the type of the data element is an integral type, divides the table into two halves, the first half has m elements, and the second half has len-m elements (1<=m<=len ), design a time complexity of O (N), the space Complexity of O (1) algorithm, change the original order table, the original m elements in the order table is placed in the back of the table, after the LEN-M elements placed in the first paragraph of the table.
Note: After the order table element is adjusted to meet the requirements of the content, and then output, the output process can only be implemented with a loop statement, can not be divided into two parts. Input the first line to enter an integer n, representing the following n lines of input;
After entering n rows, each line is preceded by an integer len and an integer m (representing the total number of elements in the table and the number of elements in the first half), followed by a Len integer representing each element of the corresponding sequential table. The output outputs have n rows, which are the result of exchanging the first m elements and after (LEN-M) elements of each sequential table with the sample Input
210 3 1 2 3 4 5 6 7 8 9 105 3 10 30 20 50 80
Sample Output
4 5 6 7 8 9 10 1 2 350 80 10 30 20
Hint: In fact, the practice of this problem has been said in the topic is very clear, the specific method is the inverse of the array, the first m elements of the inverse, and then the len-m elements of the inverse, the final unification of Len elements to achieve the inverse of the order table algorithm.
The code is implemented as follows (GCC):
#include <stdio.h>#include<stdlib.h>inta[1000010];inti,j,k,t;voidCreatlist (int*a,intN) { for(i=1; i<=n;i++) {scanf ("%d",&A[i]); }}voidSetlist (int*a,intNintm) { while(n<m) {T=A[n]; A[n]=A[m]; A[M]=T; N++; M--; }}voidPrintlist (int*a,intN) { for(i=1; i<=n;i++) {i<n? printf"%d", A[i]):p rintf ("%d", A[i]); } printf ("\ n");}intMain () {intN; intlen,m; scanf ("%d",&N); while(n--) {scanf ("%d%d",&len,&m); Creatlist (A,len); Setlist (A,1, M); Setlist (A,m+1, Len); Setlist (A,1, Len); Printlist (A,len); } return 0;}/***************************************************result:acceptedtake Time:4mstake memory:188kb************* ***************************************/
Sdut 3327 Sequential Table application 4: Inverse algorithm of element position interchange