Hdoj 1394 Minimum Inversion number

Source: Internet
Author: User
Tags hash

Original question:
Problem Description
The inversion number of a given number sequence A1, A2, ..., 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 o Btain 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
10
1 3 6 9 0 8 5 7 4 2

Sample Output
16
English:
First give you a positive integer n, then give you n number, this n number is from 0 to n-1 of free arrangement. Now ask how much you can do from the original sequence to the first element at the end, and the minimum number of reverse order.
(Note that from n-1 to 0) never read the wrong question, otherwise the answer is wrong.
Line tree code, from the online very God that hh (Helen Hu) Code ~

#include <bits/stdc++.h> using namespace std;
#define Lson l,m,rt<<1 #define Rson m+1,r,rt<<1|1//fstream in,out;
const int maxn=5501;
int sum[maxn<<2];
    void pushup (int rt) {sum[rt]=sum[rt<<1]+sum[rt<<1|1];} void build (int l,int R,int RT) {sum[rt]=0;
    if (l==r) return;
    int m= (L+R) >>1;
    Build (Lson);
Build (Rson);
        } void Update (int p,int l,int R,int rt) {if (l==r) {sum[rt]++;
    Return
    } int m= (L+R) >>1;
    if (p<=m) update (P,lson);
    else update (P,rson);
Pushup (RT);
    } int query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) return SUM[RT];
    int m= (L+R) >>1;
    int ret=0;
    if (l<=m) ret+=query (L,r,lson);
    if (r>m) ret+=query (L,r,rson);
return ret;
} int X[MAXN];
    int main () {Ios::sync_with_stdio (false);
    int n;
        while (cin>>n) {build (0,n-1,1);
    int Tmp=0,ans;    for (int i=0;i<n;i++) {cin>>x[i];
            Tmp+=query (x[i],n-1,0,n-1,1);
        Update (x[i],1,n,1);

        } ans=tmp;
            for (int i=0;i<n;i++) {tmp+= (N-1-x[i])-x[i];
        Ans=min (Tmp,ans);
    } cout<<ans<<endl;
} return 0;
 }

Tree-like array code

#include <bits/stdc++.h>
using namespace std;

FStream in,out;
const int maxn=5501;
int a[maxn],c[maxn],n;
int lowbit (int i)
{
    return i&-i;
}
void Add (int i,int x)
{
    while (i<=n)
    {
        c[i]+=x;
        I+=lowbit (i);
    }
}
int getsum (int x)
{
    int ret=0;
    while (x>0)
    {
        ret+=c[x];
        X-=lowbit (x);
    }
    return ret;
}
int main ()
{
    Ios::sync_with_stdio (false);
    while (Cin>>n)
    {
        memset (c,0,sizeof (c));
        for (int i=1;i<=n;i++)
            cin>>a[i];
        int tmp=0;
        for (int i=1;i<=n;i++)
        {
            Add (a[i]+1,1);
            Tmp+=i-getsum (A[i])-1;
        }
        int ans=tmp;
        for (int i=1;i<=n;i++)
        {
            tmp+= (n-a[i]-1)-a[i];
            Ans=min (ans,tmp);
        }
        cout<<ans<<endl;
    }
    return 0;
}

can also be sorted by a merge, slightly

Answer:
The third question of the recommended topic of the line segment tree is to think about the direction of the line tree when you first see the topic. The result wants to be biased, here also is a rise of experience, do the problem should first according to the data to think, and not first give good template or hint to think how to do.
In fact, this problem and line segment tree does not have much to do with, but the line tree and the tree array and the merge sort can be ordered in reverse order number, line tree for the topic, that is, each time the first number of the whole sequence of the last operation and there is no skill. The line tree is used only to find an inverse number.

First, the number of reverse order, line tree to find the inverse number of the method is to establish a token hash, the initialization of the hash is 0, loop n times, each time to the segment tree processing of the hash inserted a number, and marked 1, and then find in the left and right sections of this segment tree to query the new number of the first and That is, how many numbers are larger than this number. This allows the number of reverse order to be obtained in the time complexity of the NLOGN.

Next, find the minimum number of reverse order required by the topic. Here the topic is not read wrong, is 0 to n-1 the number random arrangement. To give an example,
There are 3 2 1 0 4 This 5 number, then the current reverse number is 6, then put 3 to the last surface into 2 1 0 4 {3}, you can know to put 3 on the last side, there are 3 smaller than 3 number (0,1,2) Three number will be reversed with 3, and 4 will form an inverse relationship with 3. So when the AI is put to the end of the time will reduce the number of AI reverse, but also increase the number of N-1-ai in reverse order, so that the problem is solved.

By the way, the tree array to solve the inverse number of the method, the same is to traverse N number, and then each time in the tree array to insert a[i], that is, in the tree array maintenance of C (C[a[i]), and then seek the sum of the first a[i] item, that is, to find A[i] the number of smaller than a[i) Then I minus sum is the a[i] corresponding to the number of reverse order. It is noted that 0 of the topics should be combined with 1 of all.

Their ideas by the template code to the belt run off, can be such a good study questions.

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.