HDU 1074 Doing Homework (动态规划,位运算)
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 (动态规划,位运算)的更多相关文章
- 【状态DP】 HDU 1074 Doing Homework
原题直通车:HDU 1074 Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...
- HDU 1074 Doing Homework(经典状压dp)
题目链接 Doing Homework Ignatius has just come back school from the 30th ACM/ICPC. Now he has a ...
- HDU 6186 CS Course (连续位运算)
CS Course Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- HDU 5014 Number Sequence(位运算)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 解题报告:西安网赛的题,当时想到一半,只想到从大的开始匹配,做异或运算得到对应的b[i],但是少 ...
- hdu 5491 The Next (位运算)
http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...
- HDU - 4810 - Wall Painting (位运算 + 数学)
题意: 从给出的颜料中选出天数个,第一天选一个,第二天选二个... 例如:第二天从4个中选出两个,把这两个进行异或运算(xor)计入结果 对于每一天输出所有异或的和 $\sum_{i=1}^nC_{n ...
随机推荐
- ABP+AdminLTE+Bootstrap Table权限管理系统第六节--abp控制器扩展及json封装以及6种处理时间格式化的方法
返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 一,控制器AbpController 说完了Swagger ui 我们再来说一下abp对控制器的处理和json的封 ...
- mysql主从同步(2)-问题梳理
之前详细介绍了Mysql主从复制的原理和部署过程,在mysql同步过程中会出现很多问题,导致数据同步异常.以下梳理了几种主从同步中可能存在的问题:1)slave运行过慢不能与master同步,也就是M ...
- LVS负载均衡-基础知识梳理
一. 集群的概念 服务器集群简称集群是一种服务器系统,它通过一组松散集成的服务器软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台服务器.集群系统中的单个服务器通常称 ...
- Python_复习_习题_29
# 之前做得的题 以后再遇到能保证会# 下周二考 :所有的知识# 面试题:认真对待## 三元运算符# 接收结果的变量 = 条件为真的结果 if 条件 else 条件为假的结果# 接收结果的变量 = “ ...
- Visual Studio 2013版本安装
这周老师布置了关于Visual Studio 2013版本安装过程的概述,下面我就分享给大家看吧! 首先要下载安装文件,等待下载完成之后,虽然下载文件是ios格式,但我们可以用解压缩工具解压打开.解压 ...
- 2017-2018-2 1723《程序设计与数据结构》第九周作业 & 第二周结对编程 总结
作业地址 第九次作业:https://edu.cnblogs.com/campus/besti/CS-IMIS-1723/homework/1878 (作业界面已评分,可随时查看,如果对自己的评分有意 ...
- OSGB数据压缩
OSGB数据输出时压缩数据大小,采用如下设置 osgDB::writeNodeFile(*osgbNode, "xxx/xxxx.osgb", new osgDB::Options ...
- 第三个Sprint冲刺第九天(燃尽图)
- NoSuchBeanDefinitionException:No qualifying bean of type
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException ...
- David Silver强化学习Lecture1:强化学习简介
课件:Lecture 1: Introduction to Reinforcement Learning 视频:David Silver深度强化学习第1课 - 简介 (中文字幕) 强化学习的特征 作为 ...