State Designing
概述¶
优化 dp 时,不止可以从转移过程入手,加速转移。有时,也可以从状态定义入手,通过改变设计状态的方式实现复杂度上的优化。
令人比较头疼的是,这类优化大多不具有通用性,即不能很套路地应用于多个题目中。因此,下文将从具体例题出发,力求提供思路上的启发,希望可以对读者有一定帮助。
例 1¶
题面
给定两个长度分别为
朴素的解法¶
您一眼秒了它,这不是板子吗?
定义状态
上述做法的时间复杂度
更优的解法¶
我们仔细一想,发现了一个性质:最终答案不会超过
我们又仔细一想,发现 LCS 满足贪心的性质。
更改状态定义
可以通过预处理
复杂度
例 2¶
题面
给定一个
朴素的解法¶
看到数据范围,我们考虑状压。
设
时间复杂度
更优的解法¶
上面的状态设计中,每个 bool 值,这让我们觉得有些浪费。
我们可以考虑对于每个状态 int,发现我们可以将邻接矩阵同样压缩后进行
时间复杂度
buildLast update and/or translate time of this article2026/1/7 08:56:54,Check the history
editFound smelly bugs? Translation outdated? Wanna contribute with us? Edit this Page on Github
peopleContributor of this article Enter-tainer, Xeonacid, StudyingFather, Marcythm, partychicken, Tiphereth-A, hhc0001, hqztrue, Mascros, Mout-sea
translateTranslator of this article Visit the original article!
copyrightThe article is available under CC BY-SA 4.0 & SATA ; additional terms may apply.