HDU 1394 minimum inversion number [number of reverse orders]

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394

Give N, and then give a group of 0 ~ N-1, calculate the number of reverse orders of this sequence, and the minimum value of the number of reverse orders after the exchange. The exchange rules are as follows:

A1, A2,..., An-1, an (where m = 0-the initial seqence)
A2, A3,..., An, A1 (where M = 1)
A3, A4,..., An, A1, A2 (where m = 2)
...
An, A1, A2,..., An-1 (where M = N-1)

For example, if we have obtained 4 3 2 1 0, the number of reverse orders is 10, then

4 switch to the end 3 2 1 0 4 for element 4, the number of reverse orders is reduced by 4 first, then 4-4, and the last number of reverse orders is 6

3 switch to the end 2 1 0 4 3 for element 3, the reverse order number first reduces 3, then increases 4-3, and the last Reverse Order Number is 4

Then, the minimum value of the reverse order is obtained through a loop.

// Sort the data from the merge statement to the reverse sequence # include <stdio. h> int g_ncount; void mergearray (int A [], int first, int mid, int last, int temp []) {int I = first, j = Mid + 1; int M = mid, n = last; int K = 0; while (I <= M & J <= N) // number after a [I] Number A [J] {if (a [I] <A [J]) temp [k ++] = A [I ++]; else {temp [k ++] = A [J ++]; // A [J] and each of the preceding numbers can form a reverse order number pair g_ncount + = m-I + 1 ;}} while (I <= m) temp [k ++] = A [I ++]; while (j <= N) temp [k ++] = [J ++]; for (I = 0; I <K; I ++) A [first + I] = temp [I];} void mergesort (int A [], int first, int last, int temp []) {If (first <last) {int mid = (first + last)/2; mergesort (A, first, mid, temp); // ordered mergesort (A, Mid + 1, last, temp) on the left; // ordered mergearray (A, first, mid, last, temp); // merge two more ordered columns} bool mergesort (int A [], int N) {int * P = new int [N]; if (P = NULL) return false; mergesort (A, 0, N- 1, P); Return true;} int main () {const int maxn = 5005; int A [maxn], B [maxn]; int t; while (~ Scanf ("% d", & T) {for (INT I = 0; I <t; I ++) scanf ("% d ", & A [I]), B [I] = A [I]; g_ncount = 0; mergesort (A, T); // printf ("the reverse order number pair is: % d \ n ", g_ncount); int ans = g_ncount; int sum = g_ncount; For (INT I = 0; I <t; I ++) {// printf ("% d \ n", sum); sum = sum-(B [I]) + (t-1-B [I]); if (ANS> sum) ans = sum;} printf ("% d \ n", ANS);} return 0 ;}


HDU 1394 minimum inversion number [number of reverse orders]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.