HDU 1394 Minimum Inversion number (tree-like array for reverse order)

Source: Internet
Author: User


Minimum Inversion number Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 13942 Accepted Submission (s): 8514
problem DescriptionThe inversion number of a given number sequence A1, A2, ..., is the number of pairs (AI, AJ) that satisfy I < J and Ai > AJ.
For a given sequence of numbers a1, A2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we'll Obtain another sequence. There is totally n such sequences as the following:
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)
You is asked to write a program to find the minimum inversion number out of the above sequences.
 InputThe input consists of a number of test cases. Each case consists of the lines:the first line contains a positive integer n (n <= 5000); The next line contains a permutation of the n integers from 0 to n-1.
 Outputfor each case, output the minimum inversion number in a single line.
 Sample Input
101 3 6 9 0 8 5 7 4 2
 Sample Output
16
 AuthorCHEN, Gaoli SourceZOJ Monthly, January 2003
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1394

Title: To a group of numbers, each time you can get the beginning of the number to the end, ask such a sequence of the number of reverse order of the smallest inverse value

Title Analysis: First Use the tree-like array to reverse the number, the reverse value after each Exchange can be directly calculated, because it is a 0~n-1 arrangement, so the first number (set to FIR) to the last to reduce the number of FIR in reverse order, and this group is sure to have n-fir-1 number is greater than fir, Add them again, so the number of reverse order numbers is-fir+n-fir-1

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const MAX = 5005;int c[ MAX], A[max], n;int lowbit (int x) {    return x & (-X);} void Add (int x) {for    (int i = x; i <= n; i + = Lowbit (i))        C[i] + +;} int Sum (int x) {    int res = 0;    for (int i = x; i > 0; I-= Lowbit (i))        res + = C[i];    return res;} int main () {while    (scanf ("%d", &n)! = EOF)    {        memset (c, 0, sizeof (c));        int ini = 0;        for (int i = 1; I <= n; i++)        {            scanf ("%d", &a[i]);            A[i] + +;            INI + = SUM (n)-sum (A[i]);            ADD (A[i]);        }        int mi = ini;        for (int i = 1; I <= n; i++)        {            ini + = (-(A[i]-1) + N-(A[i]-1)-1);            mi = min (mi, ini);        }        printf ("%d\n", MI);    }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1394 Minimum Inversion number (tree-like array for reverse order)

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.