HDU 1394 Minimum Inversion Number 线段树

Minimum Inversion Number
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

给n,大小是0~n-1的全排列,经过n-1次将数组的头部转移到尾部,求最小的逆序数(概念应该知道8).
我们用线段树预处理,每次输入v[i],我们就查找树中含有的比v[i]大的个数(区间为v[i]+1,n).
ep:已经插入5,现在插入4,那么比4大的有5,那么ans+1.以此类推。
经过O(nlogn)的处理我们已经可以得出最初排列的逆序数的大小,那么其他排列的大小怎么求呢。
即val=val+(n-v[i])-(v[i]-1); 我是1~n处理的全排列。
把v[i]移到末尾,那么1~n-1的全排列的逆序数大小加上n-v[i]. (即比v[i]大的数的逆序数都加上了1)
接着处理一下v[i]的逆序数。v[i]持有的逆序数范围为0~v[i]-1,即最大拥有v[i]-1个逆序数
可能你可能就会有疑问了,v[i]持有的逆序数可能小于v[i]-1啊,为啥减去v[i]-1啊。
这个其实我们已经处理过了,v[i]的逆序数不为v[i]-1,说明有比v[i]小的数排在v[i]前面。
那么这个数始终能比v[i]先处理,所以v[i]的逆序数个数一定会达到v[i]-1个。
eg:
1 3 2(逆序数分别是 1 2 1) -> 3 2 1(逆序数为2,1,0,).所以处理到v[i],v[i]的逆序数必定为v[i]-1.

public class Main {
    public static int[] sum;
    public static int[] v;
    public static void pushUp(int rt){
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    public static void update(int p,int l,int r,int rt){
        if(l==r){
            sum[rt]=1;
            return;
        }
        int mid=(l+r)>>1;
        if(p<=mid){
            update(p,l,mid,rt<<1);
        }else{
            update(p,mid+1,r,rt<<1|1);
        }
        pushUp(rt);
    }
    public static int query(int L,int R,int l,int r,int rt){
        if(L<=l&&r<=R){
            return sum[rt];
        }
        int ans=0;
        int mid=(l+r)>>1;
        if(L<=mid){
            ans+=query(L,R,l,mid,rt<<1);
        }
        if(R>mid){
            ans+=query(L,R,mid+1,r,rt<<1|1);
        }
        return ans;
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while (cin.hasNext()){
            int n=cin.nextInt();
            sum=new int[n<<2];
            v=new int[n+1];
            //build(1,n,1);
            int val=0;
            for(int i=1;i<=n;++i){
                v[i]=cin.nextInt()+1;
                val+=query(v[i]+1,n,1,n,1);
                update(v[i],1,n,1);
            }
            int ans=Integer.MAX_VALUE;
            for(int i=1;i<n;++i){
                val=val+(n-v[i])-(v[i]-1);
                ans=Math.min(ans,val);
            }
            System.out.println(ans);
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值