L - Doing Homework (状态dp)
2017-12-02 11:46
176 查看
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.
InputThe 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.
OutputFor 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
Sample Output
Hint
题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小
思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况
例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,那么我们可以枚举1~1<<n便可以得出所有的状态
然后对于每一门而言,其状态是t = 1<<i,我们看这门在现在的状态s下是不是完成,可以通过判断s&t是否为1来得到
当得出t属于s状态的时候,我们便可以进行DP了,在DP的时候要记录路径,方便之后的输出
代码:
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.
InputThe 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.
OutputFor 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.
题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小
思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况
例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,那么我们可以枚举1~1<<n便可以得出所有的状态
然后对于每一门而言,其状态是t = 1<<i,我们看这门在现在的状态s下是不是完成,可以通过判断s&t是否为1来得到
当得出t属于s状态的时候,我们便可以进行DP了,在DP的时候要记录路径,方便之后的输出
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <stack> const int inf=1<<30; using namespace std; struct node{ string name; int dead; int cost; }a[55]; struct Node{ int time; int score; int pre; int now; }dp[1<<15]; int main(){ ios::sync_with_stdio(false); int T,i,j,n; cin>>T; while(T--){ memset(dp,0,sizeof(dp)); cin>>n; for(i=0;i<n;i++) cin>>a[i].name>>a[i].dead>>a[i].cost; for(i=1;i<(1<<n);i++) { dp[i].score=inf; for(j=n-1;j>=0;j--){ if(i&(1<<j)){ int past=i-(1<<j); int ss=dp[past].time+a[j].cost-a[j].dead; if(ss<0)ss=0; if(ss+dp[past].score<dp[i].score) { dp[i].score=ss+dp[past].score; dp[i].now=j; dp[i].pre=past; dp[i].time=dp[past].time+a[j].cost; } } } } stack<int>s; int tem=(1<<n)-1; cout<<dp[tem].score<<endl; while(tem){ s.push(dp[tem].now); tem=dp[tem].pre; } while(!s.empty()){ cout<<a[s.top()].name<<endl; s.pop(); } } return 0; }
相关文章推荐
- hdoj1074 Doing Homework(好题呀,状态压缩+DP)
- 状态压缩DP-HDU-1074-Doing Homework
- kuangbin求带飞DP1 Doing HomeWork(动态规划+状态压缩)
- 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter
- hdu_1074_Doing Homework(状压DP)
- hdu1565(状态压缩dp)
- 2018年全国多校算法寒假训练营练习比赛(第二场) F 德玛西亚万岁(状态压缩DP 未解决)
- 状态压缩dp题目
- poj 2411/hdu 1400 Mondriaan's Dream 状态压缩dp
- poj3254--Fields+状态压缩dp
- POJ 2817 WordStack (状态DP)
- 状态压缩DP-Most Powerful(POJ 3471)
- USACO Section 2.3 Cow Pedigrees - DP状态还是很好找的.
- codeforces 543C C. Remembering Strings(状态压缩dp)
- POJ 1745:Divisibility 枚举某一状态的DP
- hdu 3681 Prison Break (状态压缩dp/dfs + bfs)
- dp状态压缩
- hdu3001 三进制 状态压缩dp
- bzoj 4008(浅谈全局整体考虑设计状态实现期望dp)
- hdu 4352 XHXJ's LIS --- 数位dp 状态压缩