【线段树】Minimum Inversion Number(逆序数的求解)

Minimum Inversion Number
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

这题用线段树和树状数组都可以做。要注意的有两点,一是数的大小都是0~n-1,所以数状数组和线段树都可以直接把数的大小当成范围,而不是把元素下标当范围。二是每次换序时不用重新算一遍,有规律:ans=ans-a[i]+(n-1-a[i])(即新逆序数的数目=原来逆序数的数目-要移动的数字会减少的逆序数数目+移动后增加的数目),例如把3移动到数组末尾,那么会减少3中逆序数数目(2,1,0),增加(n-1-3)种情况。

树状数组的做法是:每次输入一个数i即将其放入数状数组中,将a[i]和后面相关的加1,然后搜索1~a[i]的总和,这个总和即是比它小的数目,再用i减去总和即为比它大的数目,即逆序数的数目(注意每个输入的数都要加一再放入数状数组)

线段树的做法原理差不多,不多说了。

这题的收获是:1.扩宽了思路,线段树不一定都对应输出数组的下标,可以是数的大小

                             2.了解了逆序数的求解办法,还有一种排序的方法没研究,以后有机会再看吧

两种代码如下:

树状数组:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
int n,a[5005],c[5005];
int lowbit(int x)
{
    return x&(-x);
}
void add(int val,int i)
{
    while(i<=n)
    {
        c[i]+=val;
        i+=lowbit(i);
    }
}
int sum(int n)
{
    int ans=0;
    while(n)
    {
        ans+=c[n];
        n-=lowbit(n);
    }
    return ans;
}
int main()
{
    int i,j,ans,temp;
    while(~scanf("%d",&n))
    {
        ans=0;
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            add(1,a[i]+1);
            ans+=i-sum(a[i]+1);
        }
        temp=ans;
        for(i=1;i<=n;i++)
        {
            temp=temp-a[i]+(n-1-a[i]);
            if(temp<ans)
            ans=temp;
        }
        cout<<ans<<endl;
    }
    return 0;
}

线段树:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
const int n=5005;
struct node
{
    int l,r,num;
}t[n<<2];
void build(int l,int r,int k)
{
    t[k].l=l;
    t[k].r=r;
    t[k].num=0;
    if(l==r)
    return ;
    int mid=(l+r)>>1;
    build(l,mid,2*k);
    build(mid+1,r,2*k+1);
}
void insert(int num,int k)
{
    if(t[k].l==t[k].r)
    {
        t[k].num++;
     //   cout<<t[k].l<<" "<<t[k].r<<" "<<t[k].num<<endl;
        return ;
    }
    int mid=(t[k].l+t[k].r)>>1;
    if(num<=mid)
    insert(num,2*k);
    else
    insert(num,2*k+1);
    t[k].num=t[2*k].num+t[2*k+1].num;
}
int search(int k,int l,int r)
{
   // cout<<"f:"<<k<<" "<<t[k].l<<" "<<t[k].r<<endl;;
    if(t[k].l==l&&t[k].r==r)
    return t[k].num;
    int mid=(t[k].l+t[k].r)>>1;
    int ans=0;
    if(l>=mid+1)
    ans=search(2*k+1,l,r);
    else if(r<=mid)
    ans=search(2*k,l,r);
    else
    {
        ans+=search(2*k,l,mid);
        ans+=search(2*k+1,mid+1,r);
    }
  //  cout<<"f:"<<t[k].l<<" "<<t[k].r<<" "<<t[k].num<<endl;
    return ans;
}
int main()
{
    int i,j,ans,temp,N,a[n];
    while(~scanf("%d",&N))
    {
        build(0,N-1,1);
        ans=0;
        for(i=1;i<=N;i++)
        {
            scanf("%d",&a[i]);
            insert(a[i],1);
            ans+=i-search(1,0,a[i]);
         //   cout<<"0~"<<a[i]<<" "<<search(1,0,a[i])<<endl;
        //    cout<<ans<<endl;
        }
        temp=ans;
      //  cout<<ans<<endl;
        for(i=1;i<=N;i++)
        {
            temp=temp-a[i]+(N-a[i]-1);
         //   cout<<temp<<endl;
            if(temp<ans)
            ans=temp;
        }
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值