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. Phaserjs V2的state状态解析及技巧

    用phaserjs开发了好多游戏了,但是对phaser还是了解不深,只知道怎么去用,今天就特意花点时间研究下phaser的状态管理到底是怎么回事. 首先,new Phaser.Game,以下是Phas ...

  2. 21.django中间件源码阅读

    回顾: 关于里面的源码流程大家可以全看视频,因为代码的跳动性很大,而且会多次调用通过一方法,所以关于中间源码的部分去找个视频看一看,我写的不是很清楚. # 1 cookie session # 2 f ...

  3. Spring、SpringMVC和Springboot的区别

    spring boot就是一个大框架里面包含了许许多多的东西,其中spring就是最核心的内容之一,当然就包含spring mvc. spring mvc 是只是spring 处理web层请求的一个模 ...

  4. jquery type属性 语法

    jquery type属性 语法 作用:type 属性描述触发哪种事件类型.大理石直角尺 语法:event.typ 参数: 参数 描述 event     必需.规定要检查的事件.这个 event 参 ...

  5. 单元测试 Junit

  6. ABP .net Core MQTT+signalr通讯

    abp版本: 4.3.0.0 .net core 版本 2.2 1.Mqtt 1.1 添加程序集:M2MqttDotnetCore(差点以为没有.net core 的) 2.2 实现代码:抄了个单例模 ...

  7. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  8. AtCoder AGC032D Rotation Sort (DP)

    题目链接 https://atcoder.jp/contests/agc032/tasks/agc032_d 题解 又是一道神仙题啊啊啊啊...atcoder题真的做不来啊QAQ 第一步又是神仙转化: ...

  9. 在线PDU格式编码/解码

    在线PDU格式编码/解码 使用GSM/GPRS AT指令发送中文短信,汉字时,需要先将短信内容编码成PDU格式,然后通过AT+CMGS AT+CMGW等指令发送.     注意:需要先通过AT+CMG ...

  10. 生成json文件写入本地

    public class Json { public static void main(String[] args) { String fullPath = null; //例如:fullPath=& ...