HDU 1074 Doing Homework (dp+状态压缩)
Problem 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
题意:
有n门课程作业,每门作业的截止时间为D,需要花费的时间为C,若作业不能按时完成,每超期1天扣1分。这n门作业按课程的字典
序先后输入。
问完成这n门作业至少要扣多少分,并输出扣分最少的做作业顺序。
PS:达到扣分最少的方案有多种,请输出字典序最小的那一组方案。
分析:
因为最多只有15门课程,可以使用二进制来表示所有完成的状况。
例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,那么我们可以枚举1~1<<n便可以得出所有的状态。
然后对于每一门而言,其状态是cur = 1<<i,我们看这门在现在的状态j下是不是完成,可以通过判断cur&j是否为1来得到。
当得出cur属于j状态的时候,我们便可以进行dp了,在dp的时候要记录路径,方便之后的输出。
代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int MAXN=1<<16;
int n;
struct Node
{
char name[109];//课程名
int endTime;//结束的时间
int cost;//完成需要的时间
}course[20];
int vis[MAXN];
struct Dp
{
int cost;//时间
int pre;//前一状态
int reduced;//损失的分数
} dp[MAXN];
void out(int status)
{
int curJob=dp[status].pre^status;//当前那项工作被做的状态
int curid=0;
curJob>>=1;
while(curJob)//获得当前做的这个工作的编号
{
curid++;
curJob>>=1;
}
if(dp[status].pre!=0)//没到第一项工作就一直往前递归
{
out(dp[status].pre);
}
printf("%s\n",course[curid].name);//把这项工作的名称输出来
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%s%d%d",&course[i].name,&course[i].endTime,&course[i].cost);
dp[0].cost=0;
dp[0].pre=-1;
dp[0].reduced=0;
vis[0]=1;
for(int j=0; j<1<<n; j++) //遍历所有的状态
{
for(int work=0; work<n; work++)
{
int cur=1<<work;
if((j&cur)==0)//第work项作业没有做
{
int curTemp=cur|j;//在j的状态下又做了第work个工作
int day=dp[j].cost+course[work].cost;//需要花费的时间
dp[curTemp].cost=day;
int reduce=day-course[work].endTime;
if(reduce<0) reduce=0;//有足够的时间来完成这项作业
reduce+=dp[j].reduced;//得加上j状态下浪费得时间
if(vis[curTemp])//已经访问过
{
if(dp[curTemp].reduced>reduce)//并且扣除的分数要更新
{
dp[curTemp].reduced=min(reduce,dp[curTemp].reduced);
dp[curTemp].pre=j;
}
}
else//没有访问过
{
vis[curTemp]=1;
dp[curTemp].reduced= reduce;
dp[curTemp].pre=j;
}
}
}
}
printf("%d\n",dp[(1<<n)-1].reduced);
out((1<<n)-1);//递归输出
}
return 0;
}
HDU 1074 Doing Homework (dp+状态压缩)的更多相关文章
- HDU 1074 Doing Homework【状态压缩DP】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...
- HDU 1074 Doing Homework(状态压缩)
之前做过一个题,是在学贪心的时候做的,所以这个题就想当然的跑偏了,当看到N是<=16 的时候,状态压缩就理所当然了 #include<iostream> #include<cs ...
- HDU 1074 Doing Homework (状态压缩DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework ——(状态压缩DP)
考虑到n只有15,那么状压DP即可. 题目要求说输出字典序最小的答案的顺序,又考虑到题目给出的字符串本身字典序是递增的,那么枚举i的时候倒着来即可.因为在同样完成的情况下,后选字典序大的,小的字典序就 ...
- hdoj1074--Doing Homework (DP 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 思路: 看着数据很小,15,但是完成的顺序有15!情况,这么大的数据是无法实现的.上网查才知道要 ...
- HDU 4628 Pieces(DP + 状态压缩)
Pieces 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 题目大意:给定一个字符串s,如果子序列中有回文,可以一步删除掉它,求把整个序列删除 ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- 【状态DP】 HDU 1074 Doing Homework
原题直通车:HDU 1074 Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...
- hdu 5025 Saving Tang Monk 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...
随机推荐
- sguf冲销脚本的实现
1.该脚本为PCISS项目的sguf冲销脚本: DECLARE type typ_sguf_table is table of sguf_rowid_tab_1%rowtype ; sguf_tab ...
- 【百度】大型网站的HTTPS实践(一)——HTTPS协议和原理
大型网站的HTTPS实践(一)——HTTPS协议和原理 原创 网络通信/物联网 作者:AIOps智能运维 时间:2018-11-09 15:07:39 349 0 前言 百度于2015年上线了全站 ...
- [转帖]华为Hi 1620 等ARM 服务器版本CPU信息.
华为ARM服务器恐依赖党政输血续命 一旦制裁立马休克 http://www.sohu.com/a/240833070_99934330 几年前,ARM服务器被业界炒的火热,AMD.高通.Marvell ...
- Thread的start和run的区别
最近看到一个题目,代码如下: public static void main(String args[]) { Thread t = new Thread() { public void run() ...
- ClientDataSet字段不能进行编辑时的解决方法
ClientDataSet字段不能进行编辑时的解决方法: procedure ModifyClientDataSet(const YesOrNot: Boolean; cs : TClientDat ...
- 浅谈C++/JAVA/C#运行机制和执行效率
估计有很多同学都对C++/JAVA/C#这三大热门语言的运行机制和执行效率有或多或少的困惑,自己也有,但是经过前期的学习,了解了三者在这两方面的区别,就废话不说了,进入主题吧. 一.运 ...
- oracle表空间到32G后扩容
), ) total_space FROM dba_data_files ORDER BY tablespace_name; /*查看表空间的使用情况*/ select a.a1 表空间名称, tru ...
- 【BZOJ4247】挂饰(动态规划)
[BZOJ4247]挂饰(动态规划) 题面 BZOJ 题解 设\(f[i][j]\)表示前\(i\)个物品中还剩下\(j\)个挂钩时的最大答案. 转移显然是一个\(01\)背包,要么不选:\(f[i] ...
- 遇到问题----mongodb-----mongorestore报错too many open files甚至mongo服务崩溃
之前运行mongorestore还原mongodb数据库一直都没问题,今天还原的时候 报错too many open files.而且mongo服务经常崩溃需要重启. 问题有两方面: 原因一 一个原因 ...
- BZOJ 1497 [NOI2006]最大获利
1497: [NOI2006]最大获利 Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前 ...