[kuangbin帶你飛]專題十二 基礎DP1 D - Doing Homework HDU - 1074

ilove_Moretz發表於2020-10-05

題目描述

 Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score. 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject’s name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject’s homework).

Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.

Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.
Sample Input

2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3

Sample Output

2
Computer
Math
English
3
Computer
English
Math

Hint

In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the 
word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.

題目的大意:現在有多門課程要學習,每門課程學習需要花費時間C,每門課程有deadline D,課程沒有在deadline內學習完會扣分,晚一天,扣一分。題目的目的是求課程學習的順序,讓所扣分數最少。

思路

這道題使用狀態壓縮的dp。看著名字很唬人,但實際上狀態壓縮dp其實和普通的dp沒有區別,只不過利用了計算機二進位制儲存的特點,對於狀態只有兩種的情況,狀態壓縮dp會省空間。演算法思想上並沒有什麼不同,甚至會更簡單,狀態壓縮dp一般會進行遍歷,演算法上會更簡單。
首先解釋一下什麼是狀態壓縮。拿這道題來舉例。假設有3門課程,“000”就代表3們課程都沒有學習,“010”代表只有第二門課程學習了,“011”代表只有第一門課程沒有學習。這些二進位制的表示可以用十進位制的數字來儲存,儲存空間就很小了,比如“000”其實就是0,“010”其實就是2,“011”其實就是3。我們用一個位元組就可以表示。
接下來,我們講這道題的思路。這道題屬於動態規劃,我們就要找狀態轉移方程。舉個例子,當我們有3門課,每門課都完成的時候,狀態是“111”,那這個狀態可以由“101”,“011”,“110”三種狀態轉移而來,那麼三門課都完成,被扣分數最少就從這三種狀態中選一個,我們遍歷這三種狀態,選總扣分最小的就行。“101”,“011”,“110”這些狀態的總扣分最少的計算也是通過遍歷比較前面的狀態得來的。
所以,我們只要從“000”開始由小到大的得到計算,就能得到”111“狀態扣的最少總分。

AC程式碼

由於HDOJ的系統在維護,我的程式碼沒有辦法提交評測,這裡就暫時不放程式碼了,等系統維護完成,我的程式碼提交通過了再放上來。畢竟樣例過了,還可能會有一些邊緣資料考慮不到,會出現WA:)

相關文章