【線段樹-區間求和求最小逆序數】HDOJ Minimum Inversion Number 1394



Minimum Inversion Number

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


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
 


題意:

一個序列,求最小的逆序數,不懂逆序數的可以百度下。

解題思路:

主要就是求出第一個序列的逆序數,後面的可以遞推出來。此題暴力好像也可以過。

AC代碼:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define Lson L,Mid,Rt<<1
#define Rson Mid+1,R,Rt<<1|1

using namespace std;

const int MAXN = 5010;
int sum[MAXN<<2];
int x[MAXN];

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(R==L){
        return;
    }
    int Mid=(L+R)>>1;
    build(Lson);
    build(Rson);
}

void update(int p,int L,int R,int Rt)
{
    if(L==R){
        sum[Rt]++;
        return;
    }
    int Mid=(L+R)>>1;
    if(p<=Mid)update(p,Lson);
    else update(p,Rson);
    PushUp(Rt);
}

int query(int s,int e,int L,int R,int Rt)
{
    if(s<=L&&R<=e) return sum[Rt];
    int Mid=(L+R)>>1;
    int res=0;
    if(s<=Mid) res+=query(s,e,Lson);
    if(e>Mid) res+=query(s,e,Rson);
    return res;
}


int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        build(0,n-1,1);
        int res=0;
        for(int i=0;i<n;i++){
            scanf("%d",&x[i]);
            res+=query(x[i],n-1,0,n-1,1);
            update(x[i],0,n-1,1);
        }
        int ans=res;
        for(int i=0;i<n;i++){
            res+=n-x[i]-x[i]-1;
            ans=min(ans,res);
        }
        printf("%d\n",ans);
    }
    return 0;
}


Minimum Inversion Number

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


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
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章