本文共 2768 字,大约阅读时间需要 9 分钟。
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5238 Accepted Submission(s): 2190 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.
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.
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.
23Computer 3 3English 20 1Math 3 23Computer 3 3English 6 3Math 6 3
2ComputerMathEnglish3ComputerEnglishMath 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门课程作业,每门作业的截止时间为D,需要花费的时间为C,若作业不能按时完成,每超期1天扣1分。
这n门作业按课程的字典序先后输入
问完成这n门作业至少要扣多少分,并输出扣分最少的做作业顺序
PS:达到扣分最少的方案有多种,请输出字典序最小的那一组方案
分析:
给出的n<=15,所以可以枚举所有的状态。
用一个1<<n内的整数可以表示一个状态,例如n=3的所有状态就是 001,010,011,100,101,110,111,每一位的0和1,对应某项作业做没做。
这样可以枚举出所有的状态,剩下的就是状态之间的转移。
对于每个状态,都可以尝试完成还没有完成的作业,即把某位的0变成1. 如:001 -->101 ,001-->011
而最终还要输出作业的顺序,所以要开一个数组,在每次成功更新某个状态的同时更新路径的前驱。
然后将路径递归输出。
#include #include #include #include #include #include #define INF 0x7fffffffusing namespace std;struct node{ char s[200]; int d,c;}w[20];int dp[1<<16],time[1<<16],pre[1<<16];void init(){ for(int i=0;i< 1<<16 ;i++) { dp[i]=INF, time[i]=0, pre[i]=-1; }}void output(int x){ if(x==0) return; output(x-(1< w[j].d) //判断是否超过期限。 cost=time[i|k]-w[j].d; else cost=0; if(dp[i|k]>dp[i]+cost) //更新最少扣分,和路径前驱。 dp[i|k]=dp[i]+cost, pre[i|k]=j; } } printf("%d\n",dp[gal]); output(gal); } return 0;}
转载地址:http://warhi.baihongyu.com/