Hdu-1394-Minimum Inversion Number

Source: Internet
Author: User

Enter an integer n (n <= 5000), then input a sequence from 0 to n-1 a [1], a [2],..., a [n]: place a [1] Behind a [n], form a sequence, and place a [2] Behind a [1, into another sequence ...... Find the smallest reverse logarithm of these sequences. --> Today, I used a tree array to answer question C in the first winter training session. You only need to find the reverse logarithm of the first sequence. Here, I use bits for this purpose. 1 3 6 9 0 8 5 7 4 2 (because the node in the tree group starts from 1, add 1 and map it) 2 4 7 10 1 9 6 8 5 3, at this time, from right to left, count the reverse logarithm with cnt, access 3, the first three are 0 (not the first three sums of a), mark the position 3, add it to the BIT, and then access 5. The first five items are the sum of 1, because the position of 3 is marked with 1, cnt + = sum [5], and 1, mark the Location 5, add it to the BIT, and then access 8. The first eight items are 2, because the positions 3 and 5 are marked with a 1 ...... Then, the reverse logarithm is obtained. Www.2cto.com is followed by the reverse logarithm of the other n-1 sequences, consisting of 0 to n-1. That's great! Why? If the number of 1st is k, then the number of k on the right is smaller than it (respectively K-1, K-2 ,..., 1, 0), if the last number is k, then the number of n-k-1 on the left is greater than it (respectively k + 1, k + 2 ,..., n-1 ). Therefore, after finding the smallest reverse logarithm of the first sequence, put 1st numbers at the end of the sequence, calculate the inverse logarithm cnt-a [I] + (n-a [I]-1) of the sequence, and the minimum update value is. [Cpp] # include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int maxn = 5000 + 10; int a [maxn], C [maxn], lowerbit [maxn], n; int sum (int x) // calculate the first x and {int ret = 0; while (x> 0) {ret + = C [x]; x-= lowerbit [x];} return ret;} void add (int x, int d) // BIT modify {while (x <= n) {C [x] + = d; x + = lowerbit [x] ;}} int main () {int I; for (I = 1; I <= maxn; I ++) lowerbit [I] = I & (-I); // create a table while (~ Scanf ("% d", & n) {getchar (); for (I = 1; I <= n; I ++) scanf ("% d ", & a [I]); memset (C, 0, sizeof (C); int cnt = 0; for (I = n; I> = 1; I --) // from the back to the front {cnt + = sum (a [I] + 1); add (a [I] + 1, 1);} int MIN = cnt; for (I = 1; I <= n; I ++) MIN = min (MIN, cnt = cnt-a [I] + (n-a [I]-1); printf ("% d \ n", MIN);} 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.