HDU 1394 Minimum inversion number (line tree, violence)

Source: Internet
Author: User

Description

The 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.

Input

The 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.

Output

For 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 The problem is to ask the number of reverse order, give you a sequence number, find its reverse order, and then through the above transformation to find out the reverse number of the transformation, the output of the minimum value is the answer. Assuming that ans is the initial number of reverse order, then his next sequence of reverse number ans1=ans+n-2*a[0]-1, because the a[0] moved to the back of the number of reverse order to reduce a[0], but also to add n-a[0]-1; If this is understood, the problem is not difficult, the data is not big, the number of reverse order by line tree,   Violence can be solved by anything. Violence:
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int main () {    int n,a[ 100000],i,j,ans1,ans2;    while (~SCANF ("%d", &n))    {        ans1=0;        for (i=0;i<n;i++) scanf ("%d", &a[i]);        for (i=1;i<n;i++) for        (j=0;j<i;j++)        if (a[i]<a[j]) ans1++;        ans2=ans1;        for (i=0;i<n;i++)        {            ans1=ans1+n-2*a[i]-1;            Ans2=min (ANS1,ANS2);        }        printf ("%d\n", ans2);    }    return 0;}

  

Segment Tree

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int s[50000];struct p{ int x,y,su;};    P tree[1000000];void Build (int l,int r,int p) {tree[p].x=l;    Tree[p].y=r;    tree[p].su=0;    if (l==r) return;    int m= (L+R)/2;    Build (L,M,2*P);    Build (m+1,r,2*p+1); return;}    int find (int l,int r,int p) {if (tree[p].x==l&&tree[p].y==r) return tree[p].su;    int m= (TREE[P].X+TREE[P].Y)/2;    if (l>m) return find (l,r,2*p+1);    if (m>=r) return find (L,R,2*P); return Find (L,m,2*p) +find (m+1,r,2*p+1);}    void un (int pos,int i,int p) {tree[p].su+=i;    if (TREE[P].X==TREE[P].Y) return;    int m= (TREE[P].X+TREE[P].Y)/2;    if (pos<=m) un (pos,i,2*p); Else un (pos,i,2*p+1);}    int main () {int n,i,ans,mn;        while (~SCANF ("%d", &n)) {build (0,n-1,1);        ans=0;        for (i=0;i<n;i++) scanf ("%d", &s[i]);            for (i=0;i<n;i++) {ans+=find (s[i],n-1,1);        Un (s[i],1,1);} Mn=ans;            for (i=0;i<n;i++) {ans=ans+n-2*s[i]-1;        Mn=min (Mn,ans);    } printf ("%d\n", MN); } return 0;}

  

HDU 1394 Minimum inversion number (line tree, violence)

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.