Doing Homework

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

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

Hint

In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the
word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.

 

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

分析: 状态压缩, 用二进制表示状态,1表示有,0表示没有。

f[1<<n-1] 表示最终状态 二进制位上全为1。

此题难点在于找到前一个状态来推当前要计算的状态。

当然也容易知道 ,对于一个状态f[S]它的前一个状态为f[Ki],  {Ki在二进制位下比S少一个1}

#include <stdio.h>
#include <string.h>
#define MAXN 16
#define INF 0x7fffffff
struct tt {
int time, deadline;
char name[105];
} hw[MAXN]; struct t {
int pre, now;
int score, time;
t() {pre = -1;}
} dp[1 << MAXN]; void print(int k)
{
if(dp[k].pre!=-1)
{
print(dp[k].pre);
printf("%s\n", hw[ dp[k].now ].name );
}
}
int main()
{
int T, n, s, i, recent, past, reduce, j, max;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%s %d %d", &hw[i].name, &hw[i].deadline, &hw[i].time);
max = 1 << n;
for (s = 1; s < max; s++) {
dp[s].score = INF;
for (i = n - 1; i >= 0; i--) {
recent = 1 << i;
if (s & recent) {
past = s - recent;
reduce = dp[past].time + hw[i].time - hw[i].deadline;
if (reduce < 0)
reduce = 0;
if (reduce + dp[past].score < dp[s].score) {
dp[s].score = dp[past].score + reduce;
dp[s].now = i;
dp[s].pre = past;
dp[s].time = dp[past].time + hw[i].time;
}
}
}
}
printf("%d\n", dp[max - 1].score);
print(max-1);
}
return 0;
}

hdu1074 Doing Homework(状态压缩DP Y=Y)的更多相关文章

  1. HDU1074 Doing Homework 状态压缩dp

    题目大意: 根据完成任务的截止时间,超时一天罚1分,求完成所有任务后的最小罚时 这里n最大为15,可以利用状态压缩来解决问题 /* 首先要明白的一点是状态1/0分别表示这件事做了还是没做 而1/0的位 ...

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

    题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...

  3. D - Doing Homework 状态压缩 DP

    Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every ...

  4. Doing Homework 状态压缩DP

    Doing Homework 题目抽象:给出n个task的name,deadline,need.  每个任务的罚时penalty=finish-deadline;   task不可以同时做.问按怎样的 ...

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

    题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. #include<iostream& ...

  6. hdu1074 状态压缩dp+记录方案

    题意:       给你一些作业,每个作业有自己的结束时间和花费时间,如果超过结束时间完成,一天扣一分,问你把n个作业完成最少的扣分,要求输出方案. 思路:       状态压缩dp,记录方案数的地方 ...

  7. HDU1074(KB12-D 状态压缩dp)

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

  8. HDU1074(状态压缩DP)

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

  9. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

随机推荐

  1. 【转】 自定义iOS7导航栏背景,标题和返回按钮文字颜色

    原文:http://blog.csdn.net/mad1989/article/details/41516743 UIBarButtonItem,navigationItem,backBarButto ...

  2. XCode的一些调试技巧

    XCode 内置GDB,我们可以在命令行中使用 GDB 命令来调试我们的程序.下面将介绍一些常用的命令以及调试技巧. po 命令:为 print object 的缩写,显示对象的文本描述(显示从对象的 ...

  3. ios 中的UI控件学习总结(1)

    UIKit框架提供了非常多功能强大又易用的UI控件 下面列举一些在开发中可能用得上的UI控件 UIButton 按钮 UILabel 文本标签 UITextField 文本输入框 UIImageVie ...

  4. JVM调优实践-Tomcat调优

    调优几个重要指标 GC频率 提升每次GC的效率 准备环节 jmeter的配置 未压测前JVM配置 工程未调优前配置 -Xms400m -Xmx400m -XX:PermSize=64m -XX:Max ...

  5. hdu1102 Constructing Roads (简单最小生成树Prim算法)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  6. Mysql 应该选择什么引擎

    对于如何选择存储引擎,可以简答的归纳为一句话:“除非需要用到某些INNODB 不具备的特性,并且没有其他办法可以替代,否则都应该选择INNODB 引擎”.例如:如果要用到全文索引,建议优先考虑INNO ...

  7. 百度分享share.js插件

    //百度分享window._bd_share_config = { common : { bdText : '分享标题', bdDesc : '分享描述', bdUrl : '分享链接', bdPic ...

  8. javascript 16/1/14随记

    1.想在一个事件或者函数之后,触发某个事件. var flag=false //定义一个全局变量 function aku(){ if(flag){ } } sizemousedown=functio ...

  9. Unity扩展编辑器--类型1:Editor Windows

    Extending the Editor Unity允许你使用自己定制的inspectors和Editor Windows扩展编辑器,并且你可以使用定制的Property Drawers定义属性集在i ...

  10. sock_ntop等函数

    inet_ntop的一个基本问题是:它要求调用者传递一个指向某个二进制地址的指针, 而该地址通常包含在一个套接字地址结构中,这就要求调用者必须知道这个结构的格式和地址簇, 为了使用这个函数,我们必须为 ...