hdu1074 Doing Homework(状态压缩DP Y=Y)
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3958 Accepted Submission(s): 1577
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.
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
Computer
Math
English
3
Computer
English
Math
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)的更多相关文章
- HDU1074 Doing Homework 状态压缩dp
题目大意: 根据完成任务的截止时间,超时一天罚1分,求完成所有任务后的最小罚时 这里n最大为15,可以利用状态压缩来解决问题 /* 首先要明白的一点是状态1/0分别表示这件事做了还是没做 而1/0的位 ...
- HDU 1074 Doing Homework (状态压缩 DP)
题目大意: 有 n 项作业需要完成,每项作业有上交的期限和需完成的天数,若某项作业晚交一天则扣一分.输入每项作业时包括三部分,作业名称,上交期限,完成所需要的天数.求出完成所有作业时所扣掉的分数最少, ...
- 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 ...
- Doing Homework 状态压缩DP
Doing Homework 题目抽象:给出n个task的name,deadline,need. 每个任务的罚时penalty=finish-deadline; task不可以同时做.问按怎样的 ...
- HDU 1074 Doing Homework(状态压缩DP)
题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小 思路:二进制表示. #include<iostream& ...
- hdu1074 状态压缩dp+记录方案
题意: 给你一些作业,每个作业有自己的结束时间和花费时间,如果超过结束时间完成,一天扣一分,问你把n个作业完成最少的扣分,要求输出方案. 思路: 状态压缩dp,记录方案数的地方 ...
- HDU1074(KB12-D 状态压缩dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU1074(状态压缩DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
随机推荐
- strace 使用
- javaScript 手写图片轮播
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS屏蔽右键菜单,复制,粘帖xxxxx........
//屏蔽右键菜单 document.oncontextmenu = function (event) { if (window.event) { event = window.event; } try ...
- ubuntu忘记登录账户以及密码
笔者在诸多方面仍然是初学者.感兴趣的方面也很多,电脑装上ubuntu14.04也有一段时间了,但仍然在不断学习更多基础的东西. 因为对于命令行界面还有些不习惯,所以一直依赖于图形界面,需要使用终端的时 ...
- Jquery练手之-贪吃蛇
记得以前刚出来工作的时候,什么都不懂.老板让用Jquery写一个功能,我不会写,然后跟老板说,我就是个.net程序员,为什么要写Jquery...后面我们老大给我写了!现在我才知道net程序员要会多少 ...
- table-css
- php中json_decode()和json_encode()
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
- jdbc 连接mysql Communications link failure的解决办法
使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications link failure,The last packet successfully r ...
- Python按行读取文件
1:readline() file = open("sample.txt") while 1: line = file.readline() if not line: break ...
- lua学习笔记之-语言基础
一.基本知识 1.第一个程序: print("hello lua"); 在交互模式下直接运行: > print("hello lua") hello lu ...