ALGO-7 逆序对

ALGO-7 逆序对

题目

资源限制

时间限制:1.0s 内存限制:256.0MB

问题描述

Alice 是一个让人非常愉跃的人!他总是去学习一些他不懂的问题,然后再想出许多稀奇古怪的题目。这几天,Alice 又沉浸在逆序对的快乐当中,他已近学会了如何求逆序对对数,动态维护逆序对对数等等题目,他认为把这些题让你做简直是太没追求了,于是,经过一天的思考和完善,Alice 终于拿出了一道他认为差不多的题目:

有一颗 2n-1 个节点的二叉树,它有恰好 n 个叶子节点,每个节点上写了一个整数。如果将这棵树的所有叶子节点上的数从左到右写下来,便得到一个序列 a[1]…a[n]。现在想让这个序列中的逆序对数量最少,但唯一的操作就是选树上一个非叶子节点,将它的左右两颗子树交换。他可以做任意多次这个操作。求在最优方案下,该序列的逆序对数最少有多少。

Alice 自己已近想出了题目的正解,他打算拿来和你分享,他要求你在最短的时间内完成。

输入格式

第一行一个整数 n。
下面每行,一个数 x。

如果 x=0,表示这个节点非叶子节点,递归地向下读入其左孩子和右孩子的信息,如果 x≠0,表示这个节点是叶子节点,权值为 x。

输出格式

输出一个整数,表示最少有多少逆序对。

样例输入

3
0
0
3
1
2

样例输出

1

数据规模与约定

对于 20%的数据,n <= 5000。
对于 100%的数据,1 <= n <= 200000,0 <= a[i]<2^31。

题解

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class ALGO_7 {
    static int N = 200010;//定义总的节点个数
    static long ans = 0;//保存调整前后最小的逆序对数
    static int n, val;//n有效节点数,index为有效节点下标,val临时输入的值
    static int index = 1;
    //S保存的是以i为根节点的有效节点数目,left为i的左子树,right为i的右子树,key表示i的键值
    static int[] S = new int[N];
    static int[] left = new int[N];
    static int[] right = new int[N];
    static int[] key = new int[N];

    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    private static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    static int rRotate(int t) {//对t节点进行右旋转
        int tem = left[t];
        left[t] = right[tem];
        right[tem] = t;
        S[t] = S[left[t]] + S[right[t]] + 1;//修改其根节点的有效节点个数
        S[tem] = S[left[tem]] + S[right[tem]] + 1;
        return tem;//返回调整后的根节点
    }

    static int lRotate(int t) {
        int tem = right[t];
        right[t] = left[tem];
        left[tem] = t;
        S[t] = S[right[t]] + S[left[t]] + 1;
        S[tem] = S[right[tem]] + S[left[tem]] + 1;
        return tem;
    }

    static int adjust(int t, int flag) {//根据flag来进行何种调整(flag不能使用bool类型,c没有bool类型)
        if (flag != 0) {
            if (S[left[left[t]]] > S[right[t]] || S[right[left[t]]] > S[right[t]]) {
                if (S[right[left[t]]] > S[right[t]]) {
                    left[t] = lRotate(left[t]);
                }
                return rRotate(t);
            }
        } else {
            if (S[right[right[t]]] > S[left[t]] || S[left[right[t]]] > S[left[t]]) {
                if (S[left[right[t]]] > S[left[t]]) {
                    right[t] = rRotate(right[t]);
                }
                return lRotate(t);
            }
        }
        return t;//如果都不满足,直接返回其原先节点
    }

    static int insert(int t, int node) {
        S[t]++;//t节点数增加1
        if (key[node] < key[t]) {//根据node的值决定插入到t的左子树还是右子树
            if (left[t] == 0) {//如果左子树为空,直接接到左子树上
                left[t] = node;
            } else {//否则递归插入到左子树上
                left[t] = insert(left[t], node);
            }
        } else {
            if (right[t] == 0) {
                right[t] = node;
            } else {
                right[t] = insert(right[t], node);
            }
        }
        return adjust(t, key[node] < key[t] ? 1 : 0);//插入节点后要对平衡二叉树进行调整
    }

    static int rank(int t, int val) {//求val在以t为根节点的排名,就是求出小于val的节点个数
        if (t == 0)
            return 0;//如果节点为空,则返回0
        if (val >= key[t]) {
            return rank(right[t], val);
        } else {
            return rank(left[t], val) + 1 + S[right[t]];
        }
    }

    static int merge(int node, int begin, int end) {//把从begin-end的节点插入到大规模子树中,并且统计其总的逆序对数
        long lens = 0, rans = 0;
        int i;
        for (i = begin; i < end; i++) {//计算逆序对数
            int tem = rank(node, key[i]);
            lens += tem;
            rans += S[node] - tem;
        }
        ans += lens < rans ? lens : rans;//ans保存较小的逆序对数
        for (i = begin; i < end; i++) {//将begin-end的节点插入到以node为根节点的子树上
            left[i] = right[i] = 0;
            S[i] = 1;
            node = insert(node, i);
        }
        return node;
    }

    static int buildTree() throws IOException {
        int val = nextInt();
        if (val != 0) {
            left[index] = right[index] = 0;
            S[index] = 1;
            key[index] = val;
            return index++;
        }
        int a = index;
        int lt = buildTree();//递归创建左子树
        int b = index;
        int rt = buildTree();//递归创建右子树
        int c = index;
        if (b - a > c - b) {//b-a表示左子树的规模,c-b表示右子树的规模,将规模小的子树插入到规模更大的子树
            return merge(lt, b, c);
        } else {
            return merge(rt, a, b);
        }
    }

    public static void main(String[] args) throws IOException {
        n = nextInt();
        buildTree();
        System.out.println(ans);
    }

}
posted @ 2022-03-18 13:29  morning-start  阅读(48)  评论(0编辑  收藏  举报