题目链接

Problem Description

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

题意:

有n门课程作业,每门作业的截止时间为D,需要花费的时间为C,若作业不能按时完成,每超期1天扣1分。这n门作业按课程的字典

序先后输入。

问完成这n门作业至少要扣多少分,并输出扣分最少的做作业顺序。

PS:达到扣分最少的方案有多种,请输出字典序最小的那一组方案。

分析:

因为最多只有15门课程,可以使用二进制来表示所有完成的状况。

例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,那么我们可以枚举1~1<<n便可以得出所有的状态。

然后对于每一门而言,其状态是cur = 1<<i,我们看这门在现在的状态j下是不是完成,可以通过判断cur&j是否为1来得到。

当得出cur属于j状态的时候,我们便可以进行dp了,在dp的时候要记录路径,方便之后的输出。

代码:

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string.h>
  4. using namespace std;
  5. const int MAXN=1<<16;
  6. int n;
  7. struct Node
  8. {
  9. char name[109];//课程名
  10. int endTime;//结束的时间
  11. int cost;//完成需要的时间
  12. }course[20];
  13. int vis[MAXN];
  14. struct Dp
  15. {
  16. int cost;//时间
  17. int pre;//前一状态
  18. int reduced;//损失的分数
  19. } dp[MAXN];
  20. void out(int status)
  21. {
  22. int curJob=dp[status].pre^status;//当前那项工作被做的状态
  23. int curid=0;
  24. curJob>>=1;
  25. while(curJob)//获得当前做的这个工作的编号
  26. {
  27. curid++;
  28. curJob>>=1;
  29. }
  30. if(dp[status].pre!=0)//没到第一项工作就一直往前递归
  31. {
  32. out(dp[status].pre);
  33. }
  34. printf("%s\n",course[curid].name);//把这项工作的名称输出来
  35. }
  36. int main()
  37. {
  38. int T;
  39. scanf("%d",&T);
  40. while(T--)
  41. {
  42. memset(vis,0,sizeof(vis));
  43. scanf("%d",&n);
  44. for(int i=0; i<n; i++)
  45. scanf("%s%d%d",&course[i].name,&course[i].endTime,&course[i].cost);
  46. dp[0].cost=0;
  47. dp[0].pre=-1;
  48. dp[0].reduced=0;
  49. vis[0]=1;
  50. for(int j=0; j<1<<n; j++) //遍历所有的状态
  51. {
  52. for(int work=0; work<n; work++)
  53. {
  54. int cur=1<<work;
  55. if((j&cur)==0)//第work项作业没有做
  56. {
  57. int curTemp=cur|j;//在j的状态下又做了第work个工作
  58. int day=dp[j].cost+course[work].cost;//需要花费的时间
  59. dp[curTemp].cost=day;
  60. int reduce=day-course[work].endTime;
  61. if(reduce<0) reduce=0;//有足够的时间来完成这项作业
  62. reduce+=dp[j].reduced;//得加上j状态下浪费得时间
  63. if(vis[curTemp])//已经访问过
  64. {
  65. if(dp[curTemp].reduced>reduce)//并且扣除的分数要更新
  66. {
  67. dp[curTemp].reduced=min(reduce,dp[curTemp].reduced);
  68. dp[curTemp].pre=j;
  69. }
  70. }
  71. else//没有访问过
  72. {
  73. vis[curTemp]=1;
  74. dp[curTemp].reduced= reduce;
  75. dp[curTemp].pre=j;
  76. }
  77. }
  78. }
  79. }
  80. printf("%d\n",dp[(1<<n)-1].reduced);
  81. out((1<<n)-1);//递归输出
  82. }
  83. return 0;
  84. }

HDU 1074 Doing Homework (dp+状态压缩)的更多相关文章

  1. HDU 1074 Doing Homework【状态压缩DP】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...

  2. HDU 1074 Doing Homework(状态压缩)

    之前做过一个题,是在学贪心的时候做的,所以这个题就想当然的跑偏了,当看到N是<=16 的时候,状态压缩就理所当然了 #include<iostream> #include<cs ...

  3. HDU 1074 Doing Homework (状态压缩DP)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. HDU 1074 Doing Homework ——(状态压缩DP)

    考虑到n只有15,那么状压DP即可. 题目要求说输出字典序最小的答案的顺序,又考虑到题目给出的字符串本身字典序是递增的,那么枚举i的时候倒着来即可.因为在同样完成的情况下,后选字典序大的,小的字典序就 ...

  5. hdoj1074--Doing Homework (DP 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 思路: 看着数据很小,15,但是完成的顺序有15!情况,这么大的数据是无法实现的.上网查才知道要 ...

  6. HDU 4628 Pieces(DP + 状态压缩)

    Pieces 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 题目大意:给定一个字符串s,如果子序列中有回文,可以一步删除掉它,求把整个序列删除 ...

  7. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  8. 【状态DP】 HDU 1074 Doing Homework

    原题直通车:HDU  1074  Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...

  9. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

随机推荐

  1. windows多线程(五) 互斥量 Mutex

    一.互斥量 互斥量是windows的一个内核对象,互斥量与关键段的作用相似,可以用来确保全局资源的互斥访问.并且互斥量可以用在不同的进程中的线程互斥访问全局资源. 二.相关函数说明 使用互斥量Mute ...

  2. VNC Server (CentOS 7 GNOME)

    1. 安装VNC服务 sudo yum install tigervnc-server -y 2. 启动VNC服务,设置密码,然后停止 vncserver :1 vncserver -kill :1 ...

  3. web.config文件详解[转]

    一).Web.Config是以XML文件规范存储,配置文件分为以下格式1.配置节处理程序声明特点: 位于配置文件的顶部,包含在<configSections>标志中.2.特定应用程序配置特 ...

  4. day1 学习历程

    day1 我是一个在校大三学生,一个依然迷茫不知前景的大学混子= =,可以这么说吧 大学混子 真正开始决定好好学习大概在去年的12月份 那时经老师的提醒 开始正式接触软件开发 于是 从头开始学习语言 ...

  5. 基于JQuery的前端form表单操作

    Jquery的前端表单操作:     jquery提供了良好的方法封装,在一些基本的操作的时候,能节省很多的麻烦,其中,在具体使用时,form表单的数据提交是最频繁也最常见的前后数据交换方式,所以在前 ...

  6. C 语言assert使用

    1.assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义:#include <assert.h>void assert( i ...

  7. 调用webservice超时问题的解决[转]

    1.web.config配置,<system.web></system.web>里面增加:<httpRuntime maxRequestLength="1024 ...

  8. static关键字的总结

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...

  9. Spring、SpringMVC、MyBatis整合

    项目结构: 新建web项目:File->new->Dynamic Web Project 一.准备所需jar包1. Spring框架的jar包 spring-framework-5.0.4 ...

  10. 【hdu4285】 circuits

    http://acm.hdu.edu.cn/showproblem.php?pid=4285 (题目链接) 题意 求不不能嵌套的回路个数为K的路径方案数. Solution 插头dp,时限卡得太紧了, ...