(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
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); // recursive 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
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 ;}