Title Description:
Degree bears have an array of n numbers, and he wants to order the array from small to large, but Meng Meng's degree bears only do the following:
Either take a number in the array and place it in the last position of the array.
How many times can the minimum number of operations make the array small to large order?
Input Description:
First enter a positive integer n, and the next line enter n integers. (n<=50, the absolute value of each number is less than or equal to 1000)
Output description
Output an integer to indicate a minimum number of operations
Input Example:
4
19 7 3 25
Output Example:
3
Ideas:
First we know that the degree bears only take a number from the array and place it on the last side, and the array given to you is manually given, unknown. Therefore, it is necessary to find the degree of the bear's strategy of each operation to get the least number of operations.
You want the fewest number of operations to complete the sort, from small to large. So it's important to know what sort of a small to large one looks like.
And in the course of operation, we only need to operate when we encounter reverse order.
At this point, we found that the reverse order of the concept of existence. is the shape of "..., 3,......, 1, ..." is not satisfied from the big to the small, is reverse order, we absolutely need to operate on the large number of them.
Therefore, we find that each operation is to find the reverse order, and then to operate. Because it is necessary to sort from small to large, you have to look for the smallest reverse order. Then manipulate the larger number.
Take "19 7 3 25" For example:
The order from small to large is "3 7 19 25",
So first look at whether "3" and "7" are in reverse order,
If so, move the "7" to the last
Then continue to see whether "7" and "19" are in reverse order,
And so on
Realize:
Import Java.util.arrays;import Java.util.scanner;public class Main {public static void main (String args[]) {Scanner SC = n EW Scanner (system.in); int n = sc.nextint (); int[] a=new int[n];for (int i=0;i<a.length;i++) {a[i]=sc.nextint ();} Sc.close (); int[] B=arrays.copyofrange (A, 0, a.length); Arrays.sort (b); int count = 0;for (int j = 0; j< a.length-1;j++) {int min = b[j];int max = B[j+1];int Minnum = -1;int MAXN Um = -1;for (int i=0;i<a.length;i++) {if (a[i]==min) Minnum = i;if (a[i]==max) maxnum = i;if (minnum!=-1 && maxNum! =-1) {break;}} System.out.println ("Minnum:" +b[j]+ ":" +minnum+ "\nmaxnum:" +b[j+1]+ ":" +maxnum "); if (Maxnum<minnum) {count++;int[] AA = Arrays.copyofrange (A, 0, maxnum); int[] ab = Arrays.copyofrange (A, maxnum+1, a.length); for (int i = 0;i<aa.length;i+ +) {System.out.print (aa[i]+ "");} System.out.println (); for (int i = 0;i<ab.length;i++) {System.out.print (ab[i]+ "");} System.out.println (); int[] AC = {b[j+1]}; System.arraycopy (AB, 0, a, aa.length, ab.length); System.arraycopy (AC, 0, a, aa.length+ab.length, ac.length); for (int i = 0;i<a.length;i++) {System.out.print (a[i]+ "");} System.out.println ();}} System.out.println (count);}}
Implementing the output
4
19 7 3 25
Minnum:3:2
Maxnum:7:1
19
3 25
19 3 25 7
Minnum:7:3
maxnum:19:0
3 25 7
3 25 7 19
Minnum:19:3
Maxnum:25:1
3
7 19
3 7 19 25
3
The above shows the subscript for 3 operations, as well as the objects and intermediate processes.
Baidu Interview title: Degree of Bear sort