分类目录归档:极大化思想

贪心算法小结

写在前面

在信息学竞赛中有一类常用的算法,叫做贪心算法。所谓贪心,即对于当前的每一步决策都考虑选择最优的分支,从而使得整体最优,而对于一个问题划分成N个决策,在每次决策中选择最优的分支所需要的时间可以采用堆、左偏树、优先队列等实现(也有根据决策无需修改,在开始进行一次排序即可),进而将问题的时间复杂度大大缩减为O(NlogN)以内。贪心算法无疑是解决问题比较完美的方法,但他的局限在于无法顾及到全局,由于每次只考虑最优决策的局限性,贪心算法容易走入较差解乃至最差解的深坑中,所以在使用贪心算法前需要有对于算法正确性的严(bao)格(li)证(dui)明(pai)。笔者因为贪心学得不好,所以来补这篇文章,用于给自己做总结,希望高手能够指正文中的错误,不胜感激。

继续阅读

AIO2006 Solution

Fashion Statement

In the latest trend of skin-tight jeans and slimline handbags, having a bulging wallet or purse is simply a fashion crime. You are faced with a dilemma: either you find yourself disowned by your fashion-conscious friends, or risk carrying too little money for your taxi ride home.

You wish to carry the exact amount of money for your taxi ride (in case you are mugged or otherwise lose it). However, you also wish to use as few notes as possible to avoid being ridiculed by your friends. The notes available to you come in denominations of $1, $5, $20 and $100 (you never carry coins, which are simply far too bulky).

For example, if your taxi fare costs $67, the smallest number of notes you can carry is six — this is achieved by carrying three $20 notes, one $5 note and two $1 notes (20+20+20+5+1+1 = 67).

Your task is to determine the smallest number of notes you need to carry in order to make up a given taxi fare.

继续阅读