HDU 1394 minimum inversion number

Source: Internet
Author: User

Link: HDU 1394 minimum inversion number

This is an extension of the smallest reverse order. You can use a tree array. For a sequence of $ N $ numbers $ A $, the number of $ I $ ($ I \ in [0, n) $) the number of reverse orders $ r_ I $ can be expressed as its badge $ I $ minus the number before it and not greater than its number. For example, a [8] = 4 for the number in sequence a =. The number of reverse orders $ r_8 = 8-3 = 5 $, and the second 3 indicates three numbers that are earlier than it and smaller than it: {1, 3, 0 }. Then we can obtain the Reverse Order Number Formula for the number of $ I $:

\ Begin {equation} r_ I = I-\ sum _ {0 \ Leq j <I and a_j \ Leq a_ I} 1 \ end {equation}

For the entire sequence $ A $ has

\ Begin {equation} r = \ sum _ {0 \ Leq I <n} r_ I \ end {equation}

You can use the sum (I) function of the first I item of the tree array to quickly calculate $ \ sum _ {0 \ Leq j <I and a_j \ Leq a_ I} 1 $, and use add (I + 1) to count the number.

After obtaining the reverse Number of the initial sequence, you can easily calculate the reverse Number of the transformed sequence.

If you delete the first number $ A_1 $, the number of reverse orders is reduced by $ A_1 $.

Put the first number at the end of the series, then the number of Reverse Order will increase $ n-A_1-1 $

In this way, you can try all the transformation methods and finally obtain the minimum value.

The Code is as follows:

 1 #include <cstdlib> 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #define MAXN  5005 6 using namespace std; 7 int bit[MAXN]; 8 int arr[MAXN]; 9 int n;10 int lowbit(int x)11 {12     return x&(-x);13 }14 int sum(int x)15 {16     int ans = 0;17     while( x > 0 )18     {19         ans += bit[x];20         x -= lowbit(x);21     }22     return ans;23 }24 void add(int l, int x)25 {26     while( l <= n )27     {28         bit[l] += x;29         l += lowbit(l);30     }31 }32 int main(int argc, char *argv[])33 {34     while( scanf("%d", &n) != EOF )35     {36         int ans = 0;37         memset(bit, 0, sizeof(bit));38         for( int i = 0 ; i < n ; i++ )39         {40             scanf("%d", &arr[i]);41             ans += i - sum(arr[i]+1);42             add(arr[i]+1, 1);43         }44         int mi = ans;45         for( int i = 0 ; i < n ; i++ )46         {47             ans = ans - arr[i] + n - arr[i] -1;48             mi = ans > mi ? mi : ans;49         }50         printf("%d\n", mi);51     }52 }

 

HDU 1394 minimum inversion number

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.