题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074

题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一天则扣掉一单位学分, 要你求出完成所有作业而被扣最小的学分, 并将完成作业的顺序输出.

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

分析:(转)

刚开始以为是背包, 但背包难以记录输出顺序, 所以只能换另一种DP方式, 这里科目最大数目才15, 只要有全枚举的思想来DP就可以解决了, 有一个专有名词叫状态压缩DP. 状态压缩DP采用二制进的思想,

      1, 0分别代表有或否.

    如:

    3的二进制为 11, 则代表完成了每一,二个科目的状态, 101代表完成了第一三两个科目的状态.

    这样, 可以从0->(1 << N)来获取所有状态, 并进行适当的状态转移. 对该题来说 D[s]代表集合s的状态,  要得到D[s]的状态, 可以从0 - N 分别检查是否在s集合内[s & (1 << i) > 0则表示i在集合s上,反之..], 如果i在s集合内, 刚D[s]可从D[s-{i}]来获得, [s-{i},可以s - (1<<i)来计算]. 这样表示在已完成了s-{i}的基础上再完成i后的装态, 遍历i, 取最优解.

代码如下:

 # include<iostream>
# include<cstdio>
# include<string>
# include<cstring>
# include<stack>
using namespace std;
const int MAXN = ;
const int INF = 0xffffff;
struct Homework{
string name;
int deadline; //截止时间
int time; //完成时间
}data[MAXN]; struct {
int time; //完成该集合作业所需时间
int score; //完成该集合作业被扣学分
int last; //记录上一个位置
int pos; //记录当前位置
}dp[<<MAXN]; int main(){
int T,n;
int i;
cin>>T;
while(T--){
cin>>n;
for(i=;i<n;i++)
cin>>data[i].name>>data[i].deadline>>data[i].time;
int endstate = <<n;
int recent = ;
int reduce = ;
int past = ;
for(int S=; S<endstate; S++){
dp[S].score = INF; for(i=n-;i>=;i--){
recent = <<i;
if(S & recent){
past = S - recent; //余下的作业集合
reduce = dp[past].time + data[i].time - data[i].deadline; //完成该作业被扣学分,小于0则不扣
if(reduce < )
reduce = ;
if(reduce + dp[past].score < dp[S].score){
dp[S].time = dp[past].time + data[i].time;
dp[S].score = reduce + dp[past].score;
dp[S].pos = i;
dp[S].last = past; }
}
}
}
stack<int >path; //保存路径
recent = endstate - ; //1<<n-1,表示n个1的2进制数,即全集
while(recent){
path.push(dp[recent].pos);
recent = dp[recent].last;
}
cout << dp[endstate-].score<<endl;
while(!path.empty()){
int top = path.top();
cout<<data[top].name<<endl;
path.pop();
}
}
return ;
}

HDU 1074 Doing Homework (dp+状态压缩)的更多相关文章

  1. HDU 1074 Doing Homework (dp+状态压缩)

    题目链接 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot ...

  2. HDU 1074 Doing Homework【状态压缩DP】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意: 给定作业截止时间和完成作业所需时间,比截止时间晚一天扣一分,问如何安排作业的顺序使得最 ...

  3. HDU 1074 Doing Homework(状态压缩)

    之前做过一个题,是在学贪心的时候做的,所以这个题就想当然的跑偏了,当看到N是<=16 的时候,状态压缩就理所当然了 #include<iostream> #include<cs ...

  4. HDU 1074 Doing Homework (状态压缩DP)

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

  5. HDU 1074 Doing Homework ——(状态压缩DP)

    考虑到n只有15,那么状压DP即可. 题目要求说输出字典序最小的答案的顺序,又考虑到题目给出的字符串本身字典序是递增的,那么枚举i的时候倒着来即可.因为在同样完成的情况下,后选字典序大的,小的字典序就 ...

  6. hdoj1074--Doing Homework (DP 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 思路: 看着数据很小,15,但是完成的顺序有15!情况,这么大的数据是无法实现的.上网查才知道要 ...

  7. HDU 4628 Pieces(DP + 状态压缩)

    Pieces 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 题目大意:给定一个字符串s,如果子序列中有回文,可以一步删除掉它,求把整个序列删除 ...

  8. 【状态DP】 HDU 1074 Doing Homework

    原题直通车:HDU  1074  Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...

  9. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

随机推荐

  1. C#读取json数据介绍

    //using System.Web.Script.Serialization; JavaScriptSerializer serializer = new JavaScriptSerializer( ...

  2. Bzoj 3343: 教主的魔法 分块,二分

    3343: 教主的魔法 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 821  Solved: 364[Submit][Status][Discuss ...

  3. HW4.18

    public class Solution { public static void main(String[] args) { System.out.println("Graph 1&qu ...

  4. Mitmproxy首页、文档和下载 - 支持SSL的HTTP代理 - 开源中国社区

    Mitmproxy首页.文档和下载 - 支持SSL的HTTP代理 - 开源中国社区 undefined 利用Dnspod api批量更新添加DNS解析[python脚本] - 推酷 undefined

  5. 佛山Uber优步司机奖励政策(2月1日~2月7日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 字符串hash函数

    本文搜集了一些字符串的常用hash函数. 范例1:判断两个单词是否含有相同的字母,此时我们可以用hash做.例如,“aaabb”与"aabb"含有相同的单词.(参考:http:// ...

  7. CF 19D 线段树+set压缩坐标轴+离散化map

    题意: n个操作,在200000*200000的平面上加删点 find 严格在坐标右上角,x最小,再y最小的点 线段树做,区间为离散化后的 X轴坐标 ,维护区间点数 和 最小的 y 值 ( 维护最小y ...

  8. Winform DataTable 客户端操作数据

    //创建 DataTable DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add(" ...

  9. PostgreSQL相关的软件,库,工具和资源集合

    PostgreSQL相关的软件,库,工具和资源集合. 备份 wal-e - Simple Continuous Archiving for Postgres to S3, Azure, or Swif ...

  10. Nginx介绍 分类: Nginx 服务器搭建 2015-07-13 10:50 19人阅读 评论(0) 收藏

    海量请求,高性能服务器. 对比Apache, Apache:稳定,开源,跨平台,重量级,不支持高度并发的web服务器. 由此,出现了Lighttpd与Nignx:轻量级,高性能. 发音:engine ...