Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14600    Accepted Submission(s): 7070

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


Computer
English
Math Computer
English
Math

Sample Output

Computer
Math
English Computer
English
Math

题目大意

作业就要做不完了,每项作业(已经按照字典序排好)都有一个花费时间和限定时间,在限定时间之后在交的作业,每天扣一分,现在扣分最小值和相应的作业顺序(相同最小值时,以字典序最小的情况输出)

题目分析

注意到最多只有15个作业,所以可以利用一个二进制数来表示当前的完成情况:比如111是全完成,101是第二个没完成

假如有7个作业,那么二进制就是 1111111 也就是 2^7-1 即127 我们枚举从1到127 用dp[i]来表示达到当前状态所扣分数的最小值

然后j从后向前枚举单个作业(至于为什么从后向前之后再说),判断这个作业是不是在当前状态已经完成了的,比如101,就是第一个作业和第三个作业是已经完成了的,那么当前的状态就可以从前一个状态转化过来,比如101 就可以从100或者001转化过来 这样就可以进行更新了,显然的 dp[i]=max(dp[i],dp[前一个状态]+a[j])

现在可以解释为什么从后向前遍历单个作业了,因为根据上面的转移方程,我们可以看出来当前状态是在完成这个作业之后达到的,也就是说,这个作业是最后完成的,而题目中要求,如果遇到最小值相同的时候,要以字典序最小的方案输出,所以必须要从后往前遍历

当然,要输出做题顺序,就要每次都记录当前状态的上一个状态是什么,以及当前完成了什么作业。

代码:

#include <bits/stdc++.h>  

using namespace std; 

typedef struct
{
string name;
int dday;
int cost;
}homework; struct
{
int pre;
int name;
int vul;
int time;
}dp[<<]; homework a[];
int tot,t,n,i,j;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
{
cin>>a[i].name>>a[i].dday>>a[i].cost;
}
tot=<<n;
for(i=;i<tot;i++)
{
dp[i].vul=0x7fffff;
for(j=n-;j>=;j--)
{
int temp=<<j;
if(temp&i)
{
int s=dp[i-temp].time+a[j].cost-a[j].dday;
if(s<)
s=;
if(dp[i].vul>dp[i-temp].vul+s)
{
dp[i].pre=i-temp;
dp[i].name=j;
dp[i].vul=dp[i-temp].vul+s;
dp[i].time=dp[i-temp].time+a[j].cost;
}
}
}
}
cout<<dp[tot-].vul<<endl;
i=tot-;
stack<int>q;
while(dp[i].time)
{
q.push(dp[i].name);
i=dp[i].pre;
}
while(!q.empty())
{
cout<<a[q.top()].name<<endl;
q.pop();
}
}
}

HDU 1074 Doing Homework (状压DP)的更多相关文章

  1. HDU 1074 Doing Homework 状压dp(第一道入门题)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. HDU 1074 Doing Homework 状压DP

    由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...

  3. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  4. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu_1074_Doing Homework(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:给你n个课程(n<=15)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...

  6. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  7. HDU 1074 Doing Homework(像缩进DP)

    Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...

  8. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  9. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

随机推荐

  1. VS2015安装QT插件

    下载安装完后直接重新启动vs

  2. C++常用string函数

    来自https://www.cnblogs.com/jm-Xu/p/9318705.html string(s小写)是C++标准库中的类,纯C中没有,使用时需要包含头文件#include<str ...

  3. 快速搭建 Serverless 人脸识别离线服务

    简介 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算准 ...

  4. Linux命令-磁盘管理(一)

    Linux命令-磁盘管理(一) Linux cd命令 Linux cd命令用于切换当前工作目录至 dirName(目录参数). 其中 dirName 表示法可为绝对路径或相对路径.若目录名称省略,则变 ...

  5. ubuntu搭建、安装gitlab服务器以及初始化密码

    本为14.04 在搭建之前要确定其网络环境是没有问题.用root身份进行操作 1.安装和配置必要的依赖关系 apt-get update apt-get install -y curl openssh ...

  6. task.delay 和 thread.sleep

    1.Thread.Sleep 是同步延迟. Task.Delay异步延迟. 2.Thread.Sleep 会阻塞线程,Task.Delay不会. 3.Thread.Sleep不能取消,Task.Del ...

  7. SSH上传文件出现error

    如上图,因为我在SSH上的用户没有对Linux服务器的文件夹的修改权限,我在linux上把文件夹的权限改为可读可改,就可以了

  8. Android 图片内存优化与图片压缩

    1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFactory.decodeResource 来设置一张大图,因为这些方法在完成 ...

  9. 转载 Golang []byte与string转换的一个误区

    Golang []byte与string转换的一个误区 https://www.oyohyee.com/post/Note/golang_byte_to_string/ 2019-08-10 23:4 ...

  10. Linux-ubuntu命令-文件、磁盘管理

    .文件管理 <1>查看文件信息:ls ls是英文单词list的简写,其功能为列出目录的内容,是用户最常用的命令之一,它类似于DOS下的dir命令. Linux文件或者目录名称最长可以有26 ...