HDU 1394 -- Minimum inversion number (minimum number of reverse orders)

Source: Internet
Author: User

Question:

Given a sequence, the numbers in it are 0 to n-1. Each time we put the first number to the last number and repeat n times, what is the minimum number of reverse orders in N operations?


Ideas:

First, findInitial Number of reverse ordersAnd then move the first number to the last side, then the number of reverse orders minus the number smaller than it, plus the number larger than it. If the number I input is a [I], then the number smaller than it is a [I, the number of numbers larger than it has a n-1-a [I]

Method:Merge Sorting,Tree Array,Line Segment tree


Merge Sort code:

# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # define INF 0x3f3f3f3f # define M 5001 using namespace STD; int ans, N; int A [m], t [m], B [m]; void merge_sort (int * a, int X, int y, int * t) {If (Y-x> 1) {int M = (Y + x)> 1; int P = x, q = m, I = x; merge_sort (, x, M, T); merge_sort (a, m, Y, T); While (P <M | q <Y) {If (q> = Y | (P <M & A [p] <= A [Q]) T [I ++] = A [p ++]; else {T [I ++] = A [q ++]; ans + = m-P ;}} for (I = x; I <Y; ++ I) {A [I] = T [I] ;}}int main () {// freopen ("input.txt", "r", stdin); While (scanf ("% d ", & N )! = EOF) {ans = 0; For (INT I = 0; I <n; ++ I) {scanf ("% d", & A [I]); B [I] = A [I]; <span style = "white-space: pre"> </span>} merge_sort (B, 0, N, t ); <span style = "white-space: pre"> </span> // use a temporary array to sort the number of reverse orders, because we also need to reference the number of the original array, int Minn = ans; for (INT I = 0; I <n; ++ I) {ans + = n-1-2 * A [I]; Minn = min (Minn, ANS );} cout <Minn <Endl;} return 0 ;}



Tree array code:

# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # define INF 0x3f3f3f3f # define M 5001 using namespace STD; int C [m], A [m], n; int lowbit (int x) {return X &-X;} void Update (int x, int v) {While (x <= N) {c [x] + = V; x + = lowbit (x) ;}} int get_sum (int x) {int ans = 0; while (x> 0) {ans + = C [X]; X-= lowbit (x);} return ans;} int main () {// freopen ("input.txt", "r ", stdin); While (scanf ("% d", & N )! = EOF) {memset (C, 0, sizeof C); int ans = 0; For (INT I = 0; I <n; ++ I) {scanf ("% d", & A [I]); ans + = get_sum (N)-get_sum (A [I] + 1 ); <span style = "white-space: pre"> </span> // calculate the number of updates (A [I] +) larger than a [I );} // cout <ans <Endl; int Minn = inf; For (INT I = 0; I <n; ++ I) {ans + = n-1-2 * A [I]; Minn = min (Minn, ANS);} cout <Minn <Endl;} return 0 ;}


Line Segment Tree Code:

# Include <iostream >#include <cstring> # include <cstdio> # include <algorithm> # define M 5001 # define ls (x) x <1 # define RS (X) x <1 | 1 using namespace STD; int N; int A [m]; struct node {int L, R, num;} tre [4 * m]; void push_up (int rt) {tre [RT]. num = tre [ls (RT)]. num + tre [RS (RT)]. num;} void build (int rt, int L, int R) {tre [RT]. L = L; tre [RT]. R = r; tre [RT]. num = 0; If (L = r) return; int M = (L + r)> 1; build (LS (RT), L, M ); build (RS (RT), m + 1, R); push_up (RT);} void Update (int rt, int P, int v) {If (Tre [RT]. L = tre [RT]. r) {tre [RT]. num = V; return;} int M = (Tre [RT]. L + tre [RT]. r)> 1; if (P <= m) Update (LS (RT), P, V); else Update (RS (RT), P, V ); push_up (RT);} int get_sum (int rt, int L, int R) {If (L <= tre [RT]. L & tre [RT]. r <= r) {return tre [RT]. num;} int sum = 0; int M = (Tre [RT]. L + tre [RT]. r)> 1; if (L <= m) sum + = get_sum (LS (RT), L, R); If (M <R) sum + = get_sum (RS (RT), L, R); R Eturn sum;} int main () {// freopen ("input.txt", "r", stdin); While (scanf ("% d", & N )! = EOF) {build (, n-1); int ans = 0; For (INT I = 0; I <n; ++ I) {scanf ("% d ", & A [I]); ans + = get_sum (1, a [I] + 1, n-1 ); // calculate the number of updates (1, a [I], 1);} int Minn = ans; For (INT I = 0; I <n; ++ I) {ans + = n-1-2 * A [I]; Minn = min (Minn, ANS);} cout <Minn <Endl ;} return 0 ;}




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.