Hdu Minimum Inversion Number (a small induction about the Number of reverse orders in the line segment tree)

Source: Internet
Author: User
Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 4904 Accepted Submission (s): 2991


Problem DescriptionThe inversion number of a given number sequence a1, a2,..., an 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 will obtain another sequence. there are 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 are 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 two 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 on a single line.


Sample Input

101 3 6 9 0 8 5 7 4 2
 


Sample Output

16
 


AuthorCHEN, Gaoli


SourceZOJ Monthly, January 2003


RecommendIgnatius. L first, the idea of Reverse Order Number pairs is as follows: 1. after the entire series is obtained, we will scan from the past to the next, and count the number of such operations after a [I] is smaller than that after a [I]. In this case, we should only use n2 brute force, I did not expect a better method 2. if we count the number above a [I] and it is larger than this, we can use the input's timeliness to calculate each input number, add the num [I] value of this number to 1, and then calculate the num Sum of the numbers greater than this number, because the sum here must be larger than a [I] in this series, and the sum of the numbers that appear before it, and then add the sum to the total reverse order sum. In this case, the direct brute-force method is still n2. However, we can calculate the num number larger than this number and optimize this step, the complexity of using the line segment tree to calculate the range domain value is logn, so the overall complexity is reduced to nlogn. Let's take a look at this question. After obtaining the reverse Number of the initial series, there is a rule for finding the reverse number of other orders, that is, sum = sum + (n-1-a [I]). -a [I]: Let's verify it by yourself. I believe it is easy to get the final result. Expand it. What should I do if positive order numbers are required? It's easy. I just need to adjust the size and ask again. What should I do if I <j <k and a [I]> a [j]> a [k] Number pairs meet the requirements? Start with the number in the middle and count the logarithm m of a [I]> a [j] And the logarithm n of a [j]> a [k, m * n is... If the number of a [I]> a [j] is the same, what about the number of a [j]> a [k? Two ideas: 1. after obtaining the logarithm of a [I]> a [j], the logarithm of a [j] <a [k] is obtained. A simpler approach is to find the regular discovery, n = a number smaller than a [j] in the entire sequence-the number of numbers that are smaller than a [j] that appear before a [j] (assuming that the sequence starts from 1)) n = (a [j]-1)-(j-1-m) The question code

#include <stdio.h>#include <algorithm>using namespace std;int a[5005];struct Node{    int l,r,num;}tree[50000];void Build(int n,int x,int y){    tree[n].l = x;    tree[n].r = y;    tree[n].num = 0;    if(x == y){        return;    }    int mid = (x + y) / 2;    Build(2*n,x,mid);    Build(2*n+1,mid+1,y);}void Modify(int n,int x){    int l = tree[n].l;    int r = tree[n].r;    int mid = (l + r) / 2;    if(x == l && x == r){        tree[n].num = 1;        return;    }    if(x <= mid)    Modify(2*n,x);    else            Modify(2*n+1,x);    tree[n].num = tree[2*n].num + tree[2*n+1].num;}int Query(int n,int x,int y){    int l = tree[n].l;    int r = tree[n].r;    int mid = (l + r) / 2;    int ans = 0;;    if(x == l && y == r)        return tree[n].num;    if(x <= mid)   ans += Query(2*n,x,min(mid,y));    if(y > mid)    ans += Query(2*n+1,max(mid+1,x),y);    return ans;}int main(){    int n,sum,ans;    int i,j;    while(scanf("%d",&n) != EOF){        sum = 0;        Build(1,0,n);        for(i = 1;i <= n;i++){            scanf("%d",&a[i]);            Modify(1,a[i]);            sum += Query(1,a[i]+1,n);        }        ans = sum;        for(i = 1;i < n;i++){            sum = sum + (n - 1 - a[i]) - a[i];            if(sum < ans)                ans = sum;        }        printf("%d\n",ans);    }}

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.