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. Delphi 用ToolButton和MonthCalendar实现DateTimePicker的功能

    效果图如下: 实现平台:xp xe2,其中以上功能的实现,核心主要是参考了万一老师的资料,连接:http://www.cnblogs.com/del/archive/2011/05/12/204411 ...

  2. 键盘数字对应的ASCII码(keycode码)

    keycode 1 = 鼠标左键keycode 2 = 鼠标右键keycode 3 = Cancelkeycode 4 = 鼠标中键keycode 8 = BackSpace keycode 9 = ...

  3. javascript禁止输入数字

    function onkeypressIsNumber(){ var mainForm = document.mainForm;//mainForm是form表单的ID for(var i=0; i& ...

  4. JS冒泡事件与处理

    JavaSciprt事件中有两个很重要的特性:事件冒泡以及目标元素. 事件冒泡: 当一个元素上的事件被触发的时候,比如说鼠标点击了一个按钮,同样的事件将会在那个元素的所有祖先元素中被触发.这 一过程被 ...

  5. php 随机显示据今天30天内的任意一天

    function randomDate() { //echo date( "Y-m-d H:m:s", $newtime); //echo date("Y-m-d H:m ...

  6. Day1 初识Python

    (1)变量与赋值 name = "wanghuafeng" age = 29 print(name, age) a和b交换值 a = 3 b = 5 tmp = a a = b b ...

  7. B题(覆盖问题)

        B - B   Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Desc ...

  8. python getpass模块:隐藏不显示输入的密码

    不知道为什么,本机测试必须要在debug模式下才正常运行.. import getpass #用于隐藏用户输入的字符串,常用来接收密码 def checkuser(user,passwd): ': r ...

  9. Scut:SocketListener 的解析

    大致浏览了一遍,Scut 的网络模型采用的是 SAEA 模型, 它是 .NET Framework 3.5 开始支持的一种支持高性能 Socket 通信的实现. 通过分析 Scut 的套接字监听控制, ...

  10. JMS集群部署问题 java.net.ConnectException: Connection refused; No available router to destination

    1:本地spring配置如下 <!-- JndiTemplate --> <bean id="jndiTpl" class="org.springfra ...