HDU1074 Doing Homework(状压dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074
题意:给定有n门课的作业,每门课交作业有截止时间,和完成作业所花费的时间,如果超过规定时间完成,每超过一天就会扣1分,求一个做作业顺序要求扣的分数最少。
思路:因为数据最大是15,可以使用二进制来表示所有完成的状况,比如二进制位1001,代表第1和第4科目的作业完成,第2第3没有完成,那么从0到(1<<n)其二进制就是所有的状态了。首先枚举所有的状态,然后枚举每一门课,假如判断第i门课是否完成可以用1<< i & (当前状态)来判断,然后去更新上次的状态+上完这门课完成所花费的最小分数,dp记录状态路径,最后输出即可。
AC代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
#include<unordered_map>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = (<<)+;
struct node{
string name;
int end;
int cost;
}g[];
struct node1{
int time;
int val;
int last;
int cur;
}dp[maxn];
int m,n;
int main(){
int t;
cin>>t;
while(t--){
int n;
scanf("%d",&n);
memset(dp,,sizeof(dp));
for(int i = ;i<=n;i++) {
cin>>g[i].name ;
cin>>g[i].end>>g[i].cost;
}
int up = <<n;
for(int i = ;i<up;i++){
dp[i].val = <<;//设置花费的最大值
for(int j = n;j>=;j--){
int temp = <<(j-);//枚举第j门课是否完成
if(i & temp){//如果完成
int last = i - temp;//last为完成第j门课作业之前的状态
int s = dp[last].time + g[j].cost - g[j].end ;//完成第j门课所需要的花费
if(s<) s = ;
if(dp[last].val + s <dp[i].val ){//如果扣分少于当前的i状态,则进行更新
dp[i].cur = j; //i状态最后完成的科目是j
dp[i].val = dp[last].val + s;//更新到i状态扣的分数
dp[i].time = dp[last].time + g[j].cost;//i更新到i状态的最小时间
dp[i].last = last; //i状态的上一个状态进行更新
}
}
}
}
stack<int> s;
int temp = up - ;
printf("%d\n",dp[temp].val);//up-1为完成的状态
while(temp){
s.push(dp[temp].cur);//把路径依次读入栈中
temp = dp[temp].last;
}
while(!s.empty()){
cout<<g[s.top()].name<<endl;
s.pop();
}
}
return ;
}
HDU1074 Doing Homework(状压dp)的更多相关文章
- HDU1074 Doing Homework —— 状压DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (J ...
- hdu_1074_Doing Homework(状压DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:给你n个课程(n<=15)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...
- HDU 1074 Doing Homework 状压dp(第一道入门题)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework (状压DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework 状压DP
由于数据量较小,直接二进制模拟全排列过程,进行DP,思路由kuangbin blog得到,膜拜斌神 #include<iostream> #include<cstdio> #i ...
- kuangbin专题十二 HDU1074 Doing Homework (状压dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU1074:Doing Homework(状压DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1074 Doing Homework【状压DP】
Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...
- Doing Homework HDU - 1074 (状压dp)
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every ...
- HDU 1074:Doing Homework(状压DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Problem Description Ignatius has just ...
随机推荐
- tensorflow 中的L1和L2正则化
import tensorflow as tf weights = tf.constant([[1.0, -2.0],[-3.0 , 4.0]]) >>> sess.run(tf.c ...
- 抛弃VMware吧,使用Win10自带的Hyper-V创建虚拟机
个人博客 地址:https://www.wenhaofan.com/article/20190619221449 介绍 Hyper-V是微软提出的一种系统管理程序虚拟化技术,能够实现桌面虚拟化. 启动 ...
- gitkraken生成ssh keys并连接git
gitkraken中生成ssh keys 打开gitkraken ,点击左上file,选择preferences 需要选择一个指定目录 将复制的ssh keys粘贴到gitlab 或者 GitHub中 ...
- 简单记账本APP开发二
今天主要是进行了适配器的编写,数据库的创建以及对完善了业务逻辑,简单的APP到此已经通过测试可以使用.
- LeetCode 1046. 最后一块石头的重量 (贪心)
有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块最重的石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == y,那 ...
- 服务器的公网ip 和内网ip
原文地址:https://zhidao.baidu.com/question/814783729071869532.html 服务器公网ip 可以用于域名解析ip,服务器远程登录ip,是最主要的服务器 ...
- 0级搭建类013-CentOS 8.x 安装
CentOS 8 操作系统安装
- 【Linux02】LInux初始入门
1.LInux操作系统发展与演变 ENIARC: CUP:运算器.控制器.寄存器.缓存 存储器:内存.RAM Input:指令+数据 Output:结果 程序:指令+数据 算法+数据结构 字节是 ...
- CSS 美化网页元素
一.为什么使用CSS 1.有效的传递页面信息 2.使用CSS美化过的页面文本,使页面漂亮.美观,吸引用户 3.可以很好的突出页面的主题内容,使用户第一眼可以看到页面主要内容 4.具有良好的用户体验 二 ...
- 【巨杉数据库SequoiaDB】助力金融科技升级,巨杉数据库闪耀金融展
11月4日,以“科技助创新 开放促改革 发展惠民生”为主题的2019中国国际金融展和深圳国际金融博览会在深圳会展中心盛大开幕. 中国人民银行党委委员.副行长范一飞,深圳市人民政府副市长.党组成员艾学峰 ...