HDU--1394 Minimum Inversion Number


Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14138    Accepted Submission(s): 8625
Problem Description
The 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.
 
Input
The 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.
 
Output
For each case, output the minimum inversion number on a single line.
 
Sample Input

   
   
   
   
10 1 3 6 9 0 8 5 7 4 2

 
Sample Output

   
   
   
   
16

 
Author
CHEN, Gaoli
 
Source
ZOJ Monthly, January 2003
あなたに1つの序列をあげて、毎回第1の数を最后に移して、毎回移転する前の逆の序数を求めてそして最小のその逆の序数を探し出します
逆シーケンス定義:
1つの配列では、対数の前後の位置が大きさの順序と逆、すなわち前の数が後の数より大きい場合、それらは1つの配列と呼ばれる.
逆順.1つの配列の逆順の総数をこの配列と呼ぶ
逆序数
線分ツリーのコードを貼り付けます.
/**          :
 *            (       )         
 *                   ,1      ,0     
 *        x[i] ,        x[i] n - 1            ,           
 *                       
 *            
 *                                 
 */

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#define N 5005
using namespace std;

int Tree[N << 2];

void PushUp(int rt)
{
    Tree[rt] = Tree[rt << 1] +  Tree[rt << 1 | 1];
}
void build(int l,int r,int rt)   //     ,     0
{
    int mid;

    Tree[rt] = 0;
    if(l == r)
    {
        return;
    }
    mid = (l + r ) >> 1;
    build(l,mid,rt << 1);
    build(mid + 1,r,rt << 1 | 1);
}
void Update(int p,int l,int r,int rt)//     ,            
{
    int mid;
    if(l == r)
    {
        Tree[rt]++;//1        ,0         
        return;
    }
    mid = (l + r) >> 1;
    if(p <= mid)
    {
        Update(p,l,mid,rt << 1);
    }
    else
    {
        Update(p,mid + 1,r,rt << 1 | 1);
    }
    PushUp(rt);
}
int   Query(int ll,int rr,int l,int r,int rt)// ll rr      ,             
{
    int mid;
    int ret = 0;
    if(ll <= l && r <= rr)
    {
        return Tree[rt];
    }
    mid = (l + r) >> 1;
    if(ll <= mid) ret += Query(ll,rr,l,mid,rt << 1);
    if(rr > mid) ret += Query(ll,rr,mid + 1,r,rt << 1 | 1);
    return ret;
}

int x[N];
int main()
{
    int n;
    int sum,ret;
    int i;

    //freopen("FileIn.txt","r",stdin);
    //freopen("FileOut.txt","w",stdout);

    while(scanf("%d",&n) != EOF)
    {
        build(0,n - 1,1);//   0  ,  l = 0,r = n - 1;
        sum = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d",&x[i]);
            sum += Query(x[i],n -1 ,0,n - 1,1);//   x【i】     
            Update(x[i],0,n - 1,1);
        }
        ret = sum;//         
        for(i = 0; i < n; i++)
        {
            sum += n - x[i] - x[i] - 1;//           ,     n-1-x[i]      x[i] (      0  )
            ret = min (ret,sum);//         
        }
        printf("%d
",ret); } //fclose(stdin); //fclose(stdout); return 0; }