:::success
- 局部最优也是全局最优时才能 work
- 贪心不一定得到最优结果,而是最优解的近似解,但花费时间一般比找最优解少
- 贪心背后往往有一个更复杂的动态规划解法
:::
Activity Selection Problem
problem:选择尽可能多的相容的活动
Solutions
:::info
DP 解法
设是完全处于(起始时间都在其间)结束以后到开始之前的集合,设是中最多容纳的活动。
我们定义最优子结构为:从中选出,和也有最多的相容活动。
状态状态转移方程为:
复杂度为。
:::
:::info
贪心解法
复杂度为
:::
Theorem
Thinking
不采取之前二分的做法而是从左到右的来计算。表示从到这些活动中最多能容纳的数量,表示距离最近的与其相容的活动(也即前最晚结束的活动),因此我们的选择是放入(会导致一些之前被放入的活动被踢出去)或者不放入,在这两种选择中取最大。
如果有权重,贪心就没办法做了,因为考虑不了权重。
Huffman Codes
:::danger
- 霍夫曼树的待编码字符只出现在叶子上,也即编码是前缀的(没有任何编码是其他编码的前n位)
:::