D - Doing Homework 状态压缩 DP
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的更多相关文章
- hdu1074 Doing Homework(状态压缩DP Y=Y)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1074 Doing Homework (状态压缩 DP)
题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...
- HDU1074 Doing Homework 状态压缩dp
题目大意: 根据完成任务的截止时间,超时一天罚1分,求完成所有任务后的最小罚时 这里n最大为15,可以利用状态压缩来解决问题 /* 首先要明白的一点是状态1/0分别表示这件事做了还是没做 而1/0的位 ...
- Doing Homework 状态压缩DP
Doing Homework 题目抽象:给出n个task的name,deadline,need. 每个任务的罚时penalty=finish-deadline; task不可以同时做.问按怎样的 ...
- HDU 1074 Doing Homework(状态压缩DP)
题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. #include<iostream& ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- HDU1074(KB12-D 状态压缩dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU1074(状态压缩DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU_1074_Doing Homework_状态压缩dp
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- linux Java环境变了配置
1. sudo /etc/profile 2.安装截图配置 输入javac 进行验证
- 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- CodeDOMProvider 类
CodeDomProvider 可用来创建和检索代码生成器和代码编译器的实例.代码生成器可以生成特定语言的代码,如:C#.Visual Basic.JScript 等,而代码编译器可以将代码文件编译成 ...
- Linux用户、用户组权限管理详解
Linux用户管理三个重要文件详解: Linux登陆需要用户名.密码./etc/passwd 文件保存用户名.登录Linux时,Linux 先查找 /etc/passwd 文件中是否有这个用户名,没有 ...
- javascript学习之Date对象
前几天学习了一下date对象,于是写了一个简单的时间显示放到博客页面里(位于右上角),类似这样的效果,时:分:秒 xxxx年xx月xx日. 下面来说一下具体实现步骤. 首先,既然date是一个对象,那 ...
- Android RecyclerView 滑动时图片加载的优化
RecyclerView 滑动时的优化处理 在滑动时停止加载图片,在滑动停止时开始加载图片,这里用了Glide.pause 和Glide.resume.这里为了避免重复设置增加开销,设置了一个标志变量 ...
- DecorView 的创建
在Activity 的启动过程中,调用ActivityThread 的handleResumeActivity 方法时,先得到一个与Activity 关联的PhoneWindow 对象,然后通过Pho ...
- scroll的应用
jQuery(document).ready(function($){ $('#shang').click(function(){ $('html,body').animate({scrollTop: ...
- MySQL 8.*版本 修改root密码
MySQL .*版本 修改root密码 查看版本:select version() from dual; 1.6. 登录mysql: 登录mysql:(因为之前没设置密码,所以密码为空,不用输入密码, ...
- CAD绘制单行文字(网页版)
在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...