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][ ...
随机推荐
- Swift - 32 - 函数类型
//: Playground - noun: a place where people can play import UIKit func add(a:Int, b:Int) -> Int { ...
- JavaScript--数组--sort比较器
因为原装的sort这个API其实是先把要比较的数转换为字符串再进行比较的,所以并不好用 所以准备自定义一个比较器函数: //sort原理--->sort(arr,compare) functio ...
- SpringMVC简单搭建与入门
SpringMVC框架是spring框架的一个模块.springmvc和spring无需要通过中间整合层进行整合. 学习的时候,先了解一下流程至关重要,下面,简单介绍一下流程. 源码下载:http:/ ...
- 关于一个隐藏和显示物品列表的demo
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 将与系统时间格式不同的字符串格式化为DATETIME类型
若系统时间格式为2012/03/05 08:12:12,那么若将("2012-03-05 08:12:12")格式化为时间变量时会报错,在转化之前先将系统时间格式改变再转换就不会报 ...
- C语言学习--链表
#include "node.h" #include<stdio.h> #include<stdlib.h> //typedef struct _node ...
- 关于cmd的东西
为了督促自己,就从简单的开始学起,希望用博客来纪念我的成长 1)输入 CMD 回车.进入DOS提示符状态下.输入cd\ 回车 表示进入 c:\> 也到了C盘根目录下 2)d: 回车 是进入D盘当 ...
- UOJ 216 Jakarta Skyscrapers
http://uoj.ac/problem/216 题意:给定A,B,C,如果集合中有数i,j(i>j),那么集合就会增加i-j这个数,问有没有在初始集合为{A,B}400步内生成C的方案. 思 ...
- OI记忆口诀
splay_rotate: inline void rotate(splay_node *x){ splay_node *y,*z;int d1,d2; d1=get_parent(x,y);//三个 ...
- 【转】android MSM8974 上DeviceTree简介----不错
原文网址:http://blog.csdn.net/dongwuming/article/details/12784213 简介 主要功能是不在代码中硬编码设备信息,而是用专门的文件来描述.整个系统的 ...