hdu1394 Minimum Inversion Number 线段树求逆序数

博客探讨了如何计算序列的逆序数,重点在于理解逆序对的概念和递推关系。文章提出通过线段树和归并排序两种方法来解决,其中线段树每个节点保存区间内数字的个数,归并排序模板则在排序过程中计算逆序数。
摘要由CSDN通过智能技术生成

Minimum Inversion Number

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


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
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   1698  1540  1542  1255  2795 
 

这道题的关键是怎样求出每一个序列的逆序数,观察题目中描述的序列产生方法,可以看出每次都是将第一个元素移动到末尾,而且数字是从0到n-1且不重复,也就是说,这个序列必定是由0到n-1这n个数组成。

考虑将第一个数移动到序列末尾这个过程,因为逆序对的定义是前面的元素大于后面的元素,所以当a1在头上的时候,一定是比它小的元素可以和它组成逆序对,一共有a1对;而移动到末尾之后,只有比它大的元素才能和它组成逆序对,共有n-1-a1对。

也就是说,每次将a1移动到末尾之后,会减少a1对逆序对,增加n-1-a1对逆序对,总共增加n-1-2*a1个,这是一个递推关系,所以可以通过循环n-1次实现。

现在问题又来了,怎样求初始序列的逆序数,方法大体有三种,线段树,树状数组和归并排序,前两种都是位置相关的,也就是说是把每一个元素当成位置来处理,这里简单介绍一下如何用线段树处理:

1.首先建树,每个节点保存这个区间内的数字的个数。

2.每次输入一个数字a,查找从a到n-1这个区间的数字的个数,如果这个区间内有数字,意味着有比a大的数字在a之前,有几个数逆序对的个数就是几。

3.将数字a放到编号为3的节点,并且更新所有节点的数值。

4.最后算出的sum就是一个序列的逆序数。

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
#define MAX 20000
using namespace std;

int tree[MAX];

void PushUp(int rt)
{
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}

void Creat(int l,int r,int rt)
{
    tree[rt]=0;
    if(l==r)
    {
        return;
    }
    int m=(l+r)>>1;
    Creat(l,m,rt<<1);
    Creat(m+1,r,rt<<1|1);
}

void Update(int p,int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt]++;
        return;
    }

    int m=(l+r)>>1;
    if(p<=m)
        Update(p,l,m,rt<<1);
    else
        Update(p,m+1,r,rt<<1|1);
    PushUp(rt);
}

int Query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return tree[rt];
    }
    int m=(l+r)>>1;
    int ans=0;
    if(L<=m)
        ans+=Query(L,R,l,m,rt<<1);
    if(R>m)
        ans+=Query(L,R,m+1,r,rt<<1|1);
    return ans;
}


int main()
{
    int n,num[5050];
    int i;
    int sum;
    int Min;
    while(~scanf("%d",&n))
    {
        sum=0;
        Creat(0,n-1,1);
        for(i=0;i<n;i++)
        {
            scanf("%d",&num[i]);//输入数字
            sum+=Query(num[i],n-1,0,n-1,1);//查找在num之前并且大于它的数字的个数
            Update(num[i],0,n-1,1);//将这个数字插入到线段树中并且更新
        }
        Min=sum;
        for(i=0;i<n;i++)//迭代求逆序数
        {
            sum+=n-2*num[i]-1;
            Min=min(Min,sum);
        }
        if(Min<0)
            Min=0;
        cout<<Min<<endl;
    }
    return 0;
}


下面附上通过归并排序模板做的,注意归并排序本身需要两个数组,并且这两个数组都会发生改变,所以必须是临时的。

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
#define MAX 20000
using namespace std;

int a[5050];
int c[5050];
int num[5050];
int cnt;

void MergeSort(int l,int r)
{
    int mid,i,j,tmp;

    if(r>l+1)
    {
        mid=(l+r)/2;
        MergeSort(l,mid);
        MergeSort(mid,r);
        tmp=l;
        for(i=l,j=mid;i<mid&&j<r;)
        {
            if(a[i]>a[j])
            {
                c[tmp++]=a[j++];
                cnt+=mid-i;
            }
            else
                c[tmp++]=a[i++];
        }
        if(j<r)
            for(;j<r;j++)
            c[tmp++]=a[j];
        else
            for(;i<mid;i++)
            c[tmp++]=a[i];
        for(i=l;i<r;i++)
            a[i]=c[i];
    }
}

int main()
{
    int n;
    int i;
    //int sum;
    int Min;
    while(~scanf("%d",&n))
    {
        cnt=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&num[i]);//输入数字
            a[i]=num[i];
        }
        MergeSort(0,n);
        Min=cnt;
        for(i=0;i<n;i++)//迭代求逆序数
        {
            cnt+=n-2*num[i]-1;
            Min=min(Min,cnt);
        }
        if(Min<0)
            Min=0;
       cout<<Min<<endl;
    }
    return 0;
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值