First, look at the original question.
Microsoft 2010 Writing Test
In an arrangement, if the front and rear positions of a pair of numbers are the opposite of the size order, that is, the preceding number is greater than the number in the following, then they are called an inverse number pair. The total number of reverse order in an arrangement is called the reverse number of this permutation. As in {2,4,3,1}, 2 and 1,4 and 3,4 and 1,3 and 1 are the reverse number pairs, so the entire array has a number of reverse-order pairs of 4, now given an array, we need to count the number of reverse pairs of the array.
The simplest way to calculate the number of reverse numbers in a sequence is to count whether each number and the number following it can be used to make up the inverse number pairs. The code is as follows:
#include <stdio.h>
int main ()
{
printf (" inverse number of sequences to \ n");
printf ("-by Morewindows (http://blog.csdn.net/MoreWindows)--\n\n");
const int MAXN = 8;
int A[MAXN] = {1, 7, 2, 9, 6, 4, 5, 3};
int ncount = 0;
int I, J;
for (i = 0; i < MAXN. i++) for
(j = i + 1; j < Maxn; J +)
if (A[i] > A[j])
ncount++;
printf ("Reverse order number to:%d\n", ncount);
}
The results of the operation are as follows:
This method uses a double loop, the time complexity of O (n^2), is a less elegant method. So we try to solve it in other ways.
In the "realization of the five merging sort of the vernacular classical algorithm series", we observe the merging sort-merging sequence (1,3,5) and (2,4):
1. First, take out 1 of the previous series.
2. Then take out the 2 in the back series, obviously! This 2 and the preceding 3,5 can be composed of a reverse number pair that is 3 and 2,5 and 2 are in reverse order pairs.
3. Then take out 3 of the previous series.
4. And then take out the 4 in the back series, and the same is to know that the 4 and the 5 in the preceding series can form a pair of reverse numbers.