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

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.
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
using namespace std;
#define MAXN 16
#define INF 0x3f3f3f3f
struct node
{
string str;//作业名称
int deadline,need;//截止日和所需时间
}a[MAXN];
struct DP
{
int now,sum,pos,next;//分别是当前状态下的 时间,所扣分数,作业的下标,做的上一个作业的下标
}dp[<<MAXN]; void put_ans(int x)//递归输出答案
{
if(dp[x].next!=-)
{
put_ans(dp[x].next);
cout<<a[dp[x].pos].str<<endl;
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
cin>>a[i].str>>a[i].deadline>>a[i].need;
dp[].sum = dp[].now = ;
dp[].next = dp[].pos = -;//递归停止条件
for(int i=;i<(<<n);i++)//从0000...1 一直到 11111...1 在这里1表示作业已经完成,0表示未完成
{
dp[i].sum = INF;
for(int j=;j<n;j++)
{
if(i&(<<j))//如果当前状态下第j个为1,那么可以由第j位为零的情况转化来
{
int k = i - (<<j);
int v = dp[k].now + a[j].need - a[j].deadline;//这是在第j位为0(任务j没做)的情况下达到当前状态i所扣分
v = max(v,);
if(dp[k].sum+v<=dp[i].sum)//取最优解,在这里必须是小于等于——因为我们要保证字典序最小,所以应该尽量让字典序大的作业(j下标大的)后加入,比如110和101两种状态都能到达111而且都是最优解,那么我们应该选择101,因为在这种情况下是先完成的任务1(第1位为1)
{
dp[i].sum = dp[k].sum +v;
dp[i].now = dp[k].now + a[j].need;
dp[i].next = k;
dp[i].pos = j;
}
}
}
}
printf("%d\n", dp[(<<n)-].sum);
put_ans((<<n)-);
}
return ;
}

D - Doing Homework 状态压缩 DP的更多相关文章

  1. hdu1074 Doing Homework(状态压缩DP Y=Y)

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

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

    题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...

  3. HDU1074 Doing Homework 状态压缩dp

    题目大意: 根据完成任务的截止时间,超时一天罚1分,求完成所有任务后的最小罚时 这里n最大为15,可以利用状态压缩来解决问题 /* 首先要明白的一点是状态1/0分别表示这件事做了还是没做 而1/0的位 ...

  4. Doing Homework 状态压缩DP

    Doing Homework 题目抽象:给出n个task的name,deadline,need.  每个任务的罚时penalty=finish-deadline;   task不可以同时做.问按怎样的 ...

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

    题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. #include<iostream& ...

  6. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  7. HDU1074(KB12-D 状态压缩dp)

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

  8. HDU1074(状态压缩DP)

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

  9. HDU_1074_Doing Homework_状态压缩dp

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. swoole多进程处理产生的问题

    以前用swoole的时候,没有涉及到数据库连接,碰到问题没有那么多,后来公司业务原生来写swoole多进程,问题出现很多 1.多进程之间会产生进程隔离,global无效,不能共用一个mysql,red ...

  2. n阶完全生成图的数量

    有些事不是看到了希望才去坚持,而是坚持了才会看到希望 问题 I: 星际之门(一) 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 公元3000年,子虚帝国统 ...

  3. tomcat 参数调优

    JAVA_OPTS="-Xms2g -Xmx2g  -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath= ...

  4. 【Nodejs】记一次图像识别的冒险

    笔者的团队最近接到了一个有关图像识别的需求,本来应该由后端团队提供能力,native提供容器,前端团队仅负责一些“外围的形式工作”,不过由于各种各样的原因,最后的结果变成了前端团队在原有工作基础上,承 ...

  5. ACM_Uppercase(水题)

    Uppercase Time Limit: 2000/1000ms (Java/Others) Problem Description: 给出一句中间无特殊符号的英语句子,要求将各单词的首字母改大写. ...

  6. Kafka~消费的有效期

    消息的过期时间 我们在使用Kafka存储消息时,如果已经消费过了,再永久存储是一种资源的浪费,所有,kafka为我们提供了消息文件的过期策略,可以通过配置server.properies来实现# vi ...

  7. java项目怎么打jar包(项目中包含第三方jar包)

    1,右键选中项目,点击Export导出,找到java,选择第三个Runnable JAR file, 2,在Launch configuration中,填入程序的入口(有main方法的类),   然后 ...

  8. LR接口测试---socket

    前提条件: 编译:javac TcpServer.java 启动:java TcpServer ============================================ 代码示例: # ...

  9. Java 基础入门随笔(9) JavaSE版——文档注释

    上节中写了一些static变量以及静态的方法的定义使用以及与非静态的差别,这节补充下: 如果在一个类中所有方法都为静态的,且无成员变量,这时候需要对对应的类进行限制该类无法创建对象,具体操作如下: p ...

  10. 预测学习、深度生成式模型、DcGAN、应用案例、相关paper

    我对GAN"生成对抗网络"(Generative Adversarial Networks)的看法: 前几天在公开课听了新加坡国立大学[机器学习与视觉实验室]负责人冯佳时博士在[硬 ...