1. 程式人生 > >貪心法解決任務安排問題

貪心法解決任務安排問題

看上去一個很複雜的問題,解法卻出奇的簡單,題目描述如下(懶得翻譯了……):

We are given as input a set of n jobs, where job j has a processing time pj and a deadline dj. Recall the definition of completion times 

Cj from the video lectures. Given a schedule (i.e., an ordering of the jobs), we define the latenesslj of job j as the amount of time 
Cjdj after its deadline that the job completes, or as 0 if Cjdj. Our goal is to minimize the maximum lateness, maxjlj

. Which of the following greedy rules produces an ordering that minimizes the maximum lateness? You can assume that all processing times and deadlines are distinct

.

只要按照結束時間對任務進行升序排列,得到的便是所求的解,非常簡潔的貪心。正確性的證明,可以通過反證法,即假設最優的排列順序不是升序,那麼必定存在一個逆序對,交換這對任務,可以得到更優的解,這樣就和假設矛盾了。