切金条(哈夫曼、贪心)

这里用到的是哈夫曼编码原理,关于这个知识点的讲解可以看这位博主的,我觉得写的很好点击打开链接

       一块金条切成两半,是需要花费和长度数值一样的铜板的。比如长度为20的金条,不管切成长度多大的两半,都要花费20个铜板。一群人想整分整块金条,怎么分最省铜板?例如,给定数组{10,20,30},代表一共三个人,整块金条长度为10+20+30=60. 金条要分成10,20,30三个部分。如果,先把长度60的金条分成10和50,花费60 再把长度50的金条分成20和30,花费50一共花费110铜板。但是如果先把长度60的金条分成30和30,花费60 再把长度30金条分成10和20,花费30 一共花费90铜板。输入一个数组,返回分割的最小代价。

        贪心贪最小,利用哈夫曼原理可知,如果是要分成10, 20, 30,那么我先把10, 20加起来需要30代价(也就是30切成10,20),接着把加起来的30代价和原有的30加起来就是60代价,30+60代价就是90代价。也就是从树的顶端往下看,先是60的金条,现在先分成最大的两部分,30和30,需要60代价,接着需要其中一个30分割成10, 20,这个也需要10+20=30代价,那么一共就是90代价

直接上代码

        

import java.util.PriorityQueue;

public class test {
    public static void main(String[] args) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); // 默认小顶堆
        int[] a = new int[] {10, 20, 30};
        for (int i = 0; i < a.length; ++i) {
            pq.add(a[i]);
        }
        int sum = 0;
        while (pq.size() > 1) {
            int cur = pq.poll() + pq.poll();
            sum += cur;
            pq.add(cur);
        }
        System.out.println(sum);
    }
}

接着介绍一下建立优先级队列是小顶堆和大顶堆的做法

import java.util.Comparator;
import java.util.PriorityQueue;

public class Main {
    public static class MinheapComparator implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2; // < 0 o1 < o2 负数
        }

    }

    public static class MaxheapComparator implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1; // < o2 < o1
        }

    }

    public static void main(String[] args) {
        int[] arrForHeap = { 3, 5, 2, 7, 0, 1, 6, 4 };

        // min heap
        PriorityQueue<Integer> minQ1 = new PriorityQueue<>();
        for (int i = 0; i < arrForHeap.length; i++) {
            minQ1.add(arrForHeap[i]);
        }
        while (!minQ1.isEmpty()) {
            System.out.print(minQ1.poll() + " ");
        }
        System.out.println();

        // min heap use Comparator
        PriorityQueue<Integer> minQ2 = new PriorityQueue<>(new MinheapComparator());
        for (int i = 0; i < arrForHeap.length; i++) {
            minQ2.add(arrForHeap[i]);
        }
        while (!minQ2.isEmpty()) {
            System.out.print(minQ2.poll() + " ");
        }
        System.out.println();

        // max heap use Comparator
        PriorityQueue<Integer> maxQ = new PriorityQueue<>(new MaxheapComparator());
        for (int i = 0; i < arrForHeap.length; i++) {
            maxQ.add(arrForHeap[i]);
        }
        while (!maxQ.isEmpty()) {
            System.out.print(maxQ.poll() + " ");
        }
    }
}
========================================Talk is cheap, show me the code=======================================
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
文章作者写的matlab源代码,该文章发表在Digital Signal Processing: Ke-Kun Huang , Hui Liu, Chuan-Xian Ren, Yu-Feng Yu and Zhao-Rong Lai. Remote sensing image compression based on binary tree and optimized truncation. Digital Signal Processing, vol. 64, pp. 96-106, 2017. (http://dx.doi.org/10.1016/j.dsp.2017.02.008) 遥感图像数据非常广泛,因此需要通过空间设备上的低复杂度算法进行压缩。具有自适应扫描顺序(BTCA)的二叉树编码是一个的有效算法。然而,对于大规模遥感图像,BTCA需要大量的内存,而且不能随机存取。在本文中,我们提出了一种基于BTCA的新的编码方法。小波图像首先划分为几个块,并由BTCA单独编码的。根据BTCA的属性,仔细选择每个块的有效截断点,以优化速率失真的比例,从而获得更高的压缩比、更低的内存要求和随机访问性能。由于没有任何熵编码,所提出的方法简单快速,非常适合于空间设备。对三个遥感图像集进行实验,结果表明它可以显着提高PSNR、SSIM和VIF,以及主观视觉体验。 The remote sensing image data is so vast that it requires compression by low-complexity algorithm on space-borne equipment. Binary tree coding with adaptive scanning order (BTCA) is an effective algorithm for the mission. However, for large-scale remote sensing images, BTCA requires a lot of memory, and does not provide random access property. In this paper, we propose a new coding method based on BTCA and optimize truncation. The wavelet image is first divided into several blocks which are encoded individually by BTCA. According the property of BTCA, we select the valid truncation points for each block carefully to optimize the ratio of rate-distortion, so that a higher compression ratio, lower memory requirement and random access property are attained. Without any entropy coding, the proposed method is simple and fast, which is very suitable for space-borne equipment. Experiments are conducted on three remote sensing image sets, and the results show that it can significantly improve PSNR, SSIM and VIF, as well as subjective visual experience.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

砖业洋__

你的鼓励是我持续写作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值