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 ...
随机推荐
- Vue中vue-i18n结合vant-ui实现国际化
(一)添加依赖模块 在package.json文件中添加vant模块的依赖,如: // package.json { "dependencies": { "vant&qu ...
- 监控自己的电脑浏览器访问记录并生成csv格式
#!usr/bin/env python #-*- coding:utf-8 _*- """ @author:lenovo @file: 获取浏览器历史记录.py @ti ...
- C语言二级选择题考点汇总-数据结构与算法-【考点一】 什么是算法
1.算法及其基本特征 算法是指对方案的准确描述,是解决问题的执行步骤. 算法不等于数学上的计算方法,也不等于程序.程序是算法的载体. 算法的基本特征如下: (1)可行性:步骤可实现,执行结果可达到 ...
- 吴裕雄--天生自然 HADOOP大数据分布式处理:安装XShell
下载安装包
- 将CGCS2000的坐标值转换为WGS84的坐标值
打开图层数据,或者将已有的Excel数据导入到ArcMap中,然后打开Toolbox, ArcToolbox --> Projections and Transformations --> ...
- function_use
# 函数说明文档,help(len) def sum1(a, b): """ 求和函数sum1 :param a: 参数1 :param b: 参数2 :return: ...
- MyBatis的基本注解
MyBatis的基本注解: 增删改查 @Select("select * from teacher") public List<Teacher> selAll(); / ...
- 使用Docker镜像安装saltshaker
要求 Saltstack < 2019 Python >= 3.6 Mysql >= 5.7.8 (支持Json的Mysql都可以) Redis(无版本要求) RabbitMQ (无 ...
- day6 基础总结和编码方式
# = 赋值 == 比较值是否相等 is 比较内存地址 li1 = [1, 2, 3] li2 = li1 print(li1 is li2) print(id(li1), id(li2)) #数字, ...
- 详解 CUDA By Example 中的 Julia Set 绘制GPU优化
笔者测试环境VS2019. 基本介绍 原书作者引入Julia Sets意在使用GPU加速图形的绘制.Julia Set 是指满足下式迭代收敛的复数集合 \[ Z_{n+1}=Z_{n}^2+C \] ...