HDU 1074 Doing Homework (动态规划,位运算)

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

Http

HDU:https://vjudge.net/problem/HDU-1074

Source

动态规划,位运算,位压缩

题目大意

给定一些作业,每一种作业都有完成需要花费的时间(Costtime)和最晚完成时间(Deadline),作业每晚完成一种作业一天,就要减去一点学分,现在求如何安排做这些作业的顺序,使得减去的学分最少

解决思路

看到n的范围15,再结合本题是集合上的动态规划问题,我们可以想到用位运算来维护。为了方便操作,本题所有的数组均从0开始命名,那么n个物品就存放在0~(n-1)里。

那么我们用二进制来表示某项作业是否做了。首先可以知道的就是,初始状态是(假设以5件物品为例)00000(0),而最终状态就是11111(\(2^5-1\))。那么我们从0开始枚举每一种组合状态,然后以此减去这个状态中的1来找到它可以从哪个状态转移过来。比如10111可以从00111,10011,10101,10110转移过来。那么我们要维护那些信息呢?对于每一种组合i我们需要维护其最小的学分损失(F[i])和要做到这个最小的损失的当前最后一项作业完成的时间(Nowtime[i]),转移方程就是(设j是我们找到的可以转移过来的状态,而k是从j转移到i新做了哪项作业)

\(F[i]=min(F[j]+max(Nowtime[j]+Costtime[k]-Deadline[k],0))\)

为什么要与0取max呢?因为完成这项作业时不一定到了这项作业的最晚完成时间,所以这时Nowtime[j]+Costtime[k]-Deadline[k]是负数,而F应该增加的是0(因为没有损失学分),所以要这样做,表示至少就是0。

至于如何输出路径呢?我们可以记一个Path[i]表示组合状态i是做了那一项作业转移过来的,如i=1101,Path[i]=1就表示i是从1100转移过来的。

另外需要注意的就是,本题要求按照字典序输出,而因为其输入数据已经保证了字典序,所以是要按照输入的优先顺序输出。那么处理这个就是设置一下枚举顺序,让我们枚举新做哪一个作业的编号尽量靠后,也就是代码中的j从大到小。比如10111从10011和00111转移过来代价是一样的那么我们选择00111(这里需要理解一下)。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std; const int maxN=20;
const int inf=2147483647; int n;
char Name[maxN][200];
int Deadline[maxN];
int Costtime[maxN];
int F[1<<16];
int Nowtime[1<<16];
int Path[1<<16]; void outp(int x); int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (int i=0;i<n;i++)
cin>>Name[i]>>Deadline[i]>>Costtime[i];
int MAX=1<<n;//MAX就是最终状态+1
memset(F,127,sizeof(F));
memset(Nowtime,0,sizeof(Nowtime));
F[0]=0;//初始状态
Nowtime[0]=0;
Path[0]=-1;
for (int i=1;i<MAX;i++)//枚举每一种状态
{
for (int j=n-1;j>=0;j--)//为了保证字典序,这里要从大到小,因为要让这个新做的作业尽量靠后
{
int now=1<<j;
if ((i&now)==0)//如果这个状态不要求做第j个作业(也就是i的这一位为0),则不会从其转移过来
continue;
//cout<<i<<" "<<j<<endl;
now=now^i;//这里的now就变成了我们要找的可以转移到i的前面的状态
int delta=max(Nowtime[now]+Costtime[j]-Deadline[j],0);//delta就是计算如果做这门作业损失的学分是多少
if (F[now]+delta<F[i])//更新最优解,并记录信息
{
F[i]=F[now]+delta;
Nowtime[i]=Nowtime[now]+Costtime[j];
Path[i]=j;
}
}
}
printf("%d\n",F[MAX-1]);//输出
outp(MAX-1);//输出方案
}
return 0;
} void outp(int x)//递归输出方案
{
int k=Path[x];
if (k!=-1)
outp(x^(1<<k));
if (k==-1)
return;
printf("%s\n",Name[k]);
return;
}

HDU 1074 Doing Homework (动态规划,位运算)的更多相关文章

  1. 【状态DP】 HDU 1074 Doing Homework

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

  2. HDU 1074 Doing Homework(经典状压dp)

    题目链接  Doing Homework        Ignatius has just come back school from the 30th ACM/ICPC. Now he has a ...

  3. HDU 6186 CS Course (连续位运算)

    CS Course Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

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

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

  5. HDU 5014 Number Sequence(位运算)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少 ...

  6. hdu 5491 The Next (位运算)

    http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...

  7. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

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

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

  9. HDU - 4810 - Wall Painting (位运算 + 数学)

    题意: 从给出的颜料中选出天数个,第一天选一个,第二天选二个... 例如:第二天从4个中选出两个,把这两个进行异或运算(xor)计入结果 对于每一天输出所有异或的和 $\sum_{i=1}^nC_{n ...

随机推荐

  1. ceph学习

    网络: ceph必须要有公共网络和集群网络: public network:负责客户端交互以及osd与mon之间的通讯 cluster network:负责osd之间的复制,均衡,回填,数据恢复等操作 ...

  2. 查看服务器系统资源(cpu,内容)利用率前几位的进程的方法

    在日常运维工作中,我们经常需要了解服务器上的系统资源的使用情况,要清楚知道一些重要进程所占的资源比例.这就需要熟练掌握下面几个命令的使用: 1)查看占用CPU最高的5个进程 # ps aux | so ...

  3. linux-shell-引用-命令替换-命令退出状态-逻辑操作符

    命令替换:bash7步扩展的之一 嵌套  这里没什么意义 退出状态可以参与逻辑判断 表达式 算数表达式和条件表达式,逻辑表达式 查看passwd命令比,避免用户捕获输入密码的接口 这种方式就可以直接输 ...

  4. Week 3 结对编程

    Group: 杜正远 潘礼鹏 结对编程: 优点: 集体荣誉感.你们已经是一个集体了,一定得为对方着想负责. 1.看对方的代码,彼此会互相学习到一些奇妙的方法. 2.结对编程能把两个事情分开,降低复杂度 ...

  5. Linux内核设计与实现 第四章

    1. 什么是调度 现在的操作系统都是多任务的,为了能让更多的任务能同时在系统上更好的运行,需要一个管理程序来管理计算机上同时运行的各个任务(也就是进程). 这个管理程序就是调度程序,功能: 决定哪些进 ...

  6. RedisDump安装报错

    环境:win10 首先安装 Ruby 安装好后,使用命令行 gem install redis-dump 在安装过程中始终报错,意思是无法使用make命令 然后安装make 参考教程: http:// ...

  7. PAT L2-022 重排链表

    https://pintia.cn/problem-sets/994805046380707840/problems/994805057860517888 给定一个单链表 L​1​​→L​2​​→⋯→ ...

  8. WIN10护眼色

    参看文章:http://www.xitongcheng.com/jiaocheng/win10_article_10326.html WIN10:[HKEY_CURRENT_USER\Control ...

  9. An internal error has occurred. Java heap space

    http://stackoverflow.com/questions/11001252/running-out-of-heap-space issue: I am having a heap spac ...

  10. ComboBox中如何嵌套TreeView控件

      在ComboBox中嵌套TreeView控件,有时候我们在设计界面的时候,由于界面设计的需要,我们需要将TreeView控件嵌套在ComboBox中,因为TreeView控件实在是太占用地方了,要 ...