kuangbin专题十二 HDU1074 Doing Homework (状压dp)
Doing Homework
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12174 Accepted Submission(s): 5868
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.
题目大意:给出一系列的作业,还有每一门作业的截止日期和做完需要的时间,老师规定每超过一天,就要扣一分,让你求一个做作业的顺序,使最后扣分最少。如果扣分相同,输出字典序最小的序列。
思路:本来以为只是贪心,但是发现没有解释的过的策略。然后搜了题解,发现是状压dp,然后就放了几天,今天终于想通了。
注释详细的博客:https://blog.csdn.net/xingyeyongheng/article/details/21742341
让我有思路的博客:https://blog.csdn.net/libin56842/article/details/24316493
如果看不懂,可以先看一下我的另一篇博客。看完之后,再看这道题应该就懂了。
- --------------->>>戳这里<<<------------
dp[i] 表示达到状态i的最少扣分
首先,全排列所有作业,肯定有一组是满足要求的,但是n!很大。所以想到二进制表示一系列的状态。当然二进制并不明显看出来,这里是 1<<n, 用 1~1<<n的具体数字,它的二进制就是一系列作业的状态,1表示做了,0表示没做。枚举 1~1<<n 的所有状态,枚举 i 属于 0~n-1 ,temp = (1 << i),枚举二进制的某一位是1, 如果 s & temp != 0 那么上一状态就是 s-temp说明状态s-temp可以到达s。然后最后的 (1<<n)-1 也就是全是1(全做)的score即可
(详情见上面我的另一篇博客)
(给出两种输出代码)详情见上面dalao的博客。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define forn(i, x, n) for(int i = (x); i < n; i++)
#define nfor(i, x, n) for(int i = x-1; i >= n; i--)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+; struct node{
string name;
int end, cost;
}stu[];//初始的 struct Node{
int time, now, pre, score;
}dp[<<]; int main() {
int _, n;
for(scanf("%d", &_);_;_--) {
scanf("%d", &n);
forn(i, , n) {
cin >> stu[i].name >> stu[i].end >> stu[i].cost;
}
forn(s, , (<<n)) {//全排列所有状态
dp[s].score = inf;//刚开始全是正无穷
nfor(i, n, ) {
int temp = << i;
if(!(s & temp)) continue;//不能由做完i到达s
int past = s - temp;//如果能 past就是做完j 就到达s的状态
int st = dp[past].time + stu[i].cost - stu[i].end;//进行计算。减少的分数
if(st < )//小于0,就不用减
st = ;
if(dp[s].score > dp[past].score + st) {//更新
dp[s].score = dp[past].score + st;
dp[s].now = i;//为了输出
dp[s].pre = past;
dp[s].time = dp[past].time + stu[i].cost;
}
}
}
stack<int> S;//用栈维护输出顺序
int pos = (<<n)-;
cout << dp[pos].score << endl;
while(pos) {
S.push(dp[pos].now);
pos = dp[pos].pre;
}
while(!S.empty()) {
cout << stu[S.top()].name << endl;
S.pop();
}
}
}
const int MAX=(<<)+;
int n;
int dp[MAX],t[MAX],pre[MAX],dea[],fin[];//dp[i]记录到达状态i扣的最少分,t时相应的花去多少天了
char s[][]; void output(int x){
if(!x)return;
output(x-(<<pre[x]));
printf("%s\n",s[pre[x]]);
} if(dp[i]>dp[i-temp]+score){
dp[i]=dp[i-temp]+score;
t[i]=t[i-temp]+fin[j];//到达状态i花费的时间
pre[i]=j;//到达状态i的前驱,为了最后输出完成作业的顺序
}
kuangbin专题十二 HDU1074 Doing Homework (状压dp)的更多相关文章
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 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)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- kuangbin专题十二 POJ1661 Help Jimmy (dp)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14214 Accepted: 4729 Descr ...
- kuangbin专题十二 HDU1176 免费馅饼 (dp)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- kuangbin专题十二 HDU1260 Tickets (dp)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- java 多线程系列基础篇(四)之 synchronized关键字
1. synchronized原理 在java中,每一个对象有且仅有一个同步锁.这也意味着,同步锁是依赖于对象而存在.当我们调用某对象的synchronized方法时,就获取了该对象的同步锁.例如,s ...
- iOS多线程各种安全锁介绍 - 线程同步
一.atomic介绍 github对应Demo:https://github.com/Master-fd/LockDemo 在iOS中,@property 新增属性时,可以增加atomic选项,ato ...
- 前端seo小结,网页代码优化
seo的目的:提高网站流量 search engine optimization 搜索引擎优化seo search engine marketing 搜索引擎营销sem 权重10个等级 等级越大,权重 ...
- DAY16-Django之MTV
MTV模型 Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Template(模版):负责如何把页面展示给用户 View(视图):负责业务逻辑,并在适当的时候 ...
- 【转】webService概述
一.序言: 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是We ...
- ???Spring集成MyBatis02 【不推荐使用,了解即可】
2017年5月19日09:31:22 由于该种方法比较麻烦,所以三少暂时不更新,哈哈哈:待更新...
- C++获取电脑上连接的多个摄像头名称与编号
#include<iostream>#include "strmif.h"#include <initguid.h>#include<vector&g ...
- linux所有文件中查找关键字的命令
grep 192.168.1.1 * -r 在所有文件中查找192.168.1.1
- Linux 控制台/终端/tty/shell
一.简介 使用linux已经有一段时间,却一直弄不明白这几个概念之间的区别.这些概念本身有着非常浓厚的历史气息,随着时代的发展,他们的含义也在发生改变,它们有些已经失去了最初的含义,但是它们的名字却被 ...
- 构建一个在线ASCII视频流服务
构建一个在线ASCII视频流服务 2018-03-26 正常的文章 1685 什么是ASCII视频流服务? 其实这个名字是咱胡乱起的,具体叫啥我也不清楚,但效果如下: 大家可以在自己的命令行里试下, ...