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 ...
随机推荐
- 时间复杂度O(n^2)和O(nlog n)差距有多大?
0. 时间复杂度 接触到算法的小伙伴们都会知道时间复杂度(Time Complexity)的概念,这里先放出(渐进)时间复杂度的定义: 假设问题规模是\(n\),算法中基本操作重复执行的次数是\(n\ ...
- centos下升级git版本的操作记录
在使用git pull.git push.git clone的时候,或者在使用jenkins发版的时候,可能会报类似如下的错误: error: The requested URL returned e ...
- 如何在css中设置按钮button中包含图片文字对齐方式
<el-button class="class-management style="line-heught">班级管理
- 树莓派3代b型静态IP设置,和ssh的wlan配置
https://blog.csdn.net/qq_36305492/article/details/78607557
- sixsix团队“餐站”应用代码规范及开发文档
网络爬虫文档 以下是我们软工小组关于网络爬虫部分代码的的说明文档.至于一些分功能的小函数或方法就不在此赘述,一看就能明白.下面就主要的函数进行说明. 从总体上来说主要有三部分:店家信息爬取部分,菜品信 ...
- Linux内核第七节 20135332武西垚
预处理.编译.链接和目标文件的格式 可执行程序是怎么得来的 以C语言为例,c代码经过编译器的预处理,编译成汇编代码,由汇编器编译成目标代码,再链接成可执行文件,由操作系统加载到cpu里来执行. (截图 ...
- GIthub地址
https://github.com/cuibaoxue/Text1
- Kitematic when login show Error:Tunning socket could not be established
https://cn.bing.com/search?q=tunning+socket+could+not+be+established&qs=n&form=QBRE&sp=- ...
- about use Vue of methods
methods 处理事件 methods 在vue中处理一些逻辑方面的事情.vue事件监听的方式看上去有点违背分离的传统观念.而实际上vue中所有事件的处理方式和表达式都是严格绑定在当前的视图的vie ...
- 【Java集合的详细研究4】Java中如何遍历Map对象的4种方法
方法一 通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使 ...