[kuangbin帶你飛]專題十二 基礎DP1 D - Doing Homework HDU - 1074
題目描述
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:)
相關文章
- 專題十二 基礎DP1 題集
- [kuangbin帶你飛]專題五 並查集 題解並查集
- hdu 1789 Doing Homework again(簡單貪心)AI
- 【零基礎】帶你學C帶你飛
- [kuangbin帶你飛]專題十六 KMP & 擴充套件KMP & Manacher - I - Simpsons’ Hidden TalentsKMP套件
- 專題五 並查集【Kuangbin】並查集
- LaravelS - 基於 Swoole 加速 Laravel/Lumen - 帶你飛 ?Laravel
- 逆向基礎(十二)
- node基礎面試事件環?微任務、巨集任務?一篇帶你飛面試事件
- 專題十六 KMP & 擴充套件KMP & Manacher【Kuangbin】KMP套件
- hdu1074動態規劃狀態壓縮動態規劃
- 炸裂!MySQL 82 張圖帶你飛MySql
- Spring Cache 帶你飛(二)Spring
- Spring Cache 帶你飛(一)Spring
- vue專案首屏開啟速度慢?我來帶你飛。Vue
- 一文帶你瞭解nginx基礎Nginx
- 【基礎篇】一文帶你掌握 RedisRedis
- [JS基礎] 帶你深入瞭解JS原型JS原型
- JavaSE基礎知識分享(十二)Java
- 【IOS開發基礎系列】Cocoa基礎專題iOS
- 零基礎帶你吃掉JNI全家桶(一)
- 零基礎帶你吃掉JNI全家桶(三)
- 零基礎帶你吃掉JNI全家桶(二)
- 【基礎dp】HDU 1260 Tickets
- HDU 5212 Code (容斥 莫比烏斯反演基礎題)
- Java 基礎(十二)異常機制Java
- javascript基礎(事件的冒泡)(三十二)JavaScript事件
- javascript基礎(Math物件)(二十二)JavaScript物件
- 十二、pytorch的基礎知識PyTorch
- JavaWeb基礎總結:Servlet專題JavaWebServlet
- 帶你瞭解動態路由協議OSPF基礎路由協議
- 零基礎入門│帶你理解Kubernetes
- 帶你瞭解Typescript的14個基礎語法TypeScript
- HDU 5326 Work (基礎樹形dp)
- MySQL每秒57萬的寫入_帶你飛MySql
- 帶你學夠浪:Go語言基礎系列 - 8分鐘學基礎語法Go
- javascript基礎(延時呼叫)(四十二)JavaScript
- javascript基礎(控制流程(迴圈 while,for))(十二)JavaScriptWhile