(1) Merge and sort
Analysis:
(1) division problem: divide the sequence into two halves with equal numbers of elements.
(2) recursive solution: sort the two half elements separately.
(3) Merge: merge two ordered tables into one. (You only need to compare the minimum elements of the two sequences at a time, delete the smaller elements and add them to the merged new table)
# Include <iostream> using namespace STD; const int maxn = 1000; int A [maxn], t [maxn]; void merge_sort (int * a, int X, int y, int * t) {If (Y-x> 1) {int M = x + (Y-x)/2; // divide int P = x, q = m, I = x; merge_sort (A, X, M, T); // recursively solves merge_sort (a, m, Y, t ); // recursive while (P <M | q <Y) {If (q> = Y | (P <M & A [p] <= A [Q]) T [I ++] = A [p ++]; // copy from the left half array to the temporary space else t [I ++] = A [q ++]; // copy from the right-hand array to the temporary space} for (I = x; I <Y; ++ I) A [I] = T [I]; // copy back to a array from the auxiliary space} int main () {int N; CIN> N; For (INT I = 0; I <n; ++ I) cin> A [I]; merge_sort (A, 0, N, T); For (INT I = 0; I <n; ++ I) cout <A [I] <Endl; return 0 ;}
The two conditions in the above Code are critical!
First, as long as there is a sequence that is not empty, it is necessary to continue merging while (P <M | q <Y ).
Secondly, if (q> = Y | (P <M & A [p] <= A [Q]).
(1) If the second sequence is empty, the first sequence must not be empty at this time, otherwise it cannot enter the while loop. Copy A [p] at this time.
(2) otherwise (the second sequence is not empty), when and only when the first sequence is not empty and a [p] <= A [Q, to copy a [p].
(2) solving the reverse logarithm using the merge sort idea
Question: Calculate the reverse logarithm of a column!
Analysis: Since N may be large, the enumeration method of O (n2) definitely times out. So we need to use the divide and conquer method!
(1) division problem: divide the sequence into two halves with equal numbers of elements.
(2) recursive solution: calculate the number of reverse pairs on the left or right of both I and J.
(3) Merge: count the number of reverse pairs on the left and on the right of J.
After Division, for each J on the right, count the number of elements F (j) greater than the left, then the sum of all F (j) is the answer.
Therefore, because our Merge Sorting operations are performed from small to large, when a [J] on the right is copied to T, the numbers that have not been copied to T on the left are all the numbers larger than a [J] on the left.
Therefore, you can add M-P to the number of elements on the left in the accumulators!
# Include <iostream> using namespace STD; const int maxn = 1000; int A [maxn], t [maxn]; void inverse_pair (int * a, int X, int y, int * CNT, int * t) {If (Y-x> 1) {int M = x + (Y-x)/2; // divide int P = X, Q = m, I = x; inverse_pair (A, X, M, CNT, T); // recursive inverse_pair (a, m, Y, CNT, t ); // recursive while (P <M | q <Y) {If (q> = Y | (P <M & A [p] <= A [Q]) T [I ++] = A [p ++]; // copy from the left half array to the temporary space else {T [I ++] = A [q ++]; // copy the array from the right to the temporary space * CNT + = m-P; // update the accumulators} for (I = x; I <Y; ++ I) A [I] = T [I]; // copy back to a array from the auxiliary space} int main () {int N; CIN> N; for (INT I = 0; I <n; ++ I) CIN> A [I]; int CNT = 0; inverse_pair (A, 0, N, & CNT, t); cout <CNT <Endl; return 0 ;}