第一次做这道题大概是半个月前了吧,状压DP一个很新鲜的名词

当时看题解怎么也看不懂,现在看懂了以后还是很简单的

所谓状态压缩就是用一个整数的二进制来表示一个状态,比如有三个作业

000表示一科作业也没做,001表示只做了第一科,111表示三科作业都做了

那么从状态0开始出发,遍历每一个状态,如果对于状态S有第i科作业没写那么计算该状态下做完第i科作业对应的扣分数,如果比别人状态下转移过来的扣分数要少,那么状态S就是下一个状态的前驱

用结构体里的pre来保存路径

最后输出科目的时候用递归输出,也是一种很常用的方法

 //#define LOCAL
#include <cstdio>
#include <cstring> const int maxn = ( << );
struct Node
{
int used;
int pre;
int reduced;
}dp[maxn]; struct Course
{
int deadline;
int cost;
char name[];
}course[]; bool vis[maxn]; void OutPut(int S)
{
int t = S ^ dp[S].pre;
int i = -;
while(t)
{
t >>= ;
++i;
}
if(dp[S].pre != )
OutPut(dp[S].pre);
printf("%s\n", course[i].name);
} int main(void)
{
#ifdef LOCAL
freopen("1074in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%s%d%d", course[i].name, &course[i].deadline, &course[i].cost);
memset(vis, false, sizeof(vis));
vis[] = true;
dp[].used = , dp[].pre = -, dp[].reduced = ;
int All = ( << n) - ;
for(int S = ; S < All; ++S)
for(int i = ; i < n; ++i)
{
if((S & ( << i)) == )
{//S状态下第i项作业还没做
Node temp;
int next = S | ( << i);
temp.used = dp[S].used + course[i].cost;
temp.pre = S;
temp.reduced = temp.used - course[i].deadline;
if(temp.reduced < ) temp.reduced = ;
temp.reduced += dp[S].reduced; if(vis[next] && temp.reduced < dp[next].reduced)
dp[next] = temp;
else if(!vis[next])
{
vis[next] = true;
dp[next] = temp;
}
}
} printf("%d\n", dp[All].reduced);
OutPut(All);
}
return ;
}

代码君

HDU 1074 Doing Homework的更多相关文章

  1. 【状态DP】 HDU 1074 Doing Homework

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

  2. HDU 1074 Doing Homework (动态规划,位运算)

    HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...

  4. HDU 1074 Doing Homework(状压DP)

    第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...

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

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

  6. 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 ...

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

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

  8. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

  9. HDU 1074 Doing Homework(经典状压dp)

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

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

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

随机推荐

  1. 报名|「OneAPM x DaoCloud」技术公开课:Docker性能监控!

    如今,越来越多的公司开始 Docker 了,「三分之二的公司在尝试了 Docker 后最终使用了它」,也就是说 Docker 的转化率达到了 67%,同时转化时长也控制在 60 天内. 既然 Dock ...

  2. C#&java重学笔记(面向对象)

    C#部分 1.C#有一个internal关键字,指字段可以同一个程序集中访问,出了程序集不行.还有一个protected internal(没有先后之分)修饰词,指只能在同一个程序集中的子类访问 2. ...

  3. (转)Fibonacci Tilings

    Fibonacci numbers {Fn, n ≥ 0} satisfy the recurrence relation (1) Fn+2 = Fn+1 + Fn, along with the i ...

  4. SQL技术内幕-8 使用WITH AS提高性能简化嵌套SQL

    --本文来源:http://www.cnblogs.com/fygh/archive/2011/08/31/2160266.html 一.WITH AS的含义 WITH AS短语,也叫做子查询部分(s ...

  5. GridControl Find/Clear 添加图标

    public static void ControlFind(GridControl grid) { FindControl fControl = null; foreach (Control ite ...

  6. POJ 2007 Scrambled Polygon (简单极角排序)

    题目链接 题意 : 对输入的点极角排序 思路 : 极角排序方法 #include <iostream> #include <cmath> #include <stdio. ...

  7. BZOJ 3224: Tyvj 1728 普通平衡树 vector

    3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...

  8. mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换等

    mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换(上) MP3文件格式(二)---ID3v2 图:ID3V1标签结构 图:ID3V2标签 ...

  9. iOS开发--storyboard适配pin

  10. WPF之通过EventTrigger修改模板中元素的属性

    前言:对于此操作,我只想说是微软的神经,还是我的笨蛋.为什么EventTrigger就不能像Trigger那样直接设置Property以及Value就对属性进行操作,而必须要放一个Action,而默认 ...