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 ...
随机推荐
- PairProject 电梯调度 【附加题】
[附加题] 改进电梯调度的interface 设计, 让它更好地反映现实, 更能让学生练习算法, 更好地实现信息隐藏和信息共享. 目前的设计有什么缺点, 你会如何改进它? 1.之前判断电梯是否闲置的函 ...
- 实验三 Java敏捷开发与xp实现
实验内容: 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实 ...
- 第六周分析Linux内核创建一个新进程的过程
潘恒 原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 task_struct结构: ...
- JAVA链表中迭代器的实现
注:本文代码出自<java数据结构和算法>一书. PS:本文中类的名字定义存在问题,Link9应改为Link.LinkList9应该为LinkList.由于在同包下存在该名称,所以在后面接 ...
- BugPhobia进阶篇章:系统架构技术规格
0x01 :开发级需求分析 在开发过程中,团队本身在开发的起始阶段确定了基本的开发级需求分析: 在开发过程中,除了需要满足用户级需求以为,我们还需要针对开发团队的特点,满足一些开发级的需求和约束.作为 ...
- PAT 1047 编程团体赛
https://pintia.cn/problem-sets/994805260223102976/problems/994805277163896832 编程团体赛的规则为:每个参赛队由若干队员组成 ...
- Eclipse集成Tomcat报错:java.lang.OutOfMemoryError: PermGen space
Eclipse集成Tomcat报错,使用Spring 4.3 框架,运行一段应用后,控制台报错: Unexpected death of background thread ContainerBack ...
- IE下JS保存图片
function ieSave() { var img = document.images[0]; ...
- 面象对象设计原则之四:接口隔离原则(The Interface Segregation Principle,ISP)
接口隔离原则定义如下: 接口隔离原则(Interface Segregation Principle, ISP):使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 根 ...
- flask+mako+peewee(下)(解决了Error 2006: MySQL server has gone away)
这篇主要介绍在这次项目中使用的peewee 文档地址:http://peewee.readthedocs.org/en/latest/index.html 首先我们要初始化一个数据库连接对象.这里我使 ...