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. [C++ STL] 迭代器(iterator)详解

    背景:指针可以用来遍历存储空间连续的数据结构,但是对于存储空间非连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历.因此,我们引入迭代器概念.   一.迭代器(iterator)介绍 ...

  2. webview页面间的通信问题

    前提 记一次多页面开发. 开发需求时会对页面刷新(reload),返回到上一页(用户返回 / history.go()) 页面间的通信 sessionStorage保存本次会话的信息,同步到新页面或上 ...

  3. protobuf的lua版

    推荐个protobuf的lua版     以前项目客户端lua,通信协议是protobuf,用网易的proto-gen-lua,使用过程遇到些问题需要绕,比如: 1.每次更改.增加proto都要生成新 ...

  4. STL容器迭代过程中删除元素技巧(转)

    1.连续内存序列容器(vector,string,deque) 序列容器的erase方法返回值是指向紧接在被删除元素之后的元素的有效迭代器,可以根据这个返回值来安全删除元素. vector<in ...

  5. android视频播放器系列(二)——VideoView

    最近在学习视频相关的知识,现在也是在按部就班的一步步的来,如果有同样需求的同学可以跟着大家一起促进学习. 上一节说到了可以使用系统播放器以及浏览器播放本地以及网络视频,但是这在很大程度上并不能满足我们 ...

  6. Burp Suite抓https数据包

    本地环境JDK1.8Burp Suite 1.7.26 Firefox 59.0.2 一.burp介绍请自行谷歌,这里不过多介绍 二.配置HTTPS抓包方法[以Firefox为例]通常情况下burp默 ...

  7. struts2之actionSupport学习

    actionSupport在手工完成字段验证,显示错误消息,国际化等情况下推荐使用.

  8. Verilog之event

    1 Explicit event The value changes on nets and variable can be used as events to trigger the executi ...

  9. Spring框架之控制反转和依赖注入

    学Spring框架必须理解控制反转和依赖注入.下面各自举一个例子,来说明控制反转和依赖注入. IOC(控制反转):应用本身创建和维护的依赖对象:现在交由外部容器(Spring)来创建和维护:这个控制权 ...

  10. Oracl常用e函数整理

    最近学Oracle数据库,常常遇到Oracle数据库函数问题,经过默默地琢磨处理,总结了一些Oracle数据库常用函数. ------------------------------------ -- ...