Doing Homework

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12174    Accepted Submission(s): 5868

Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
 
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
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.

 
Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.
 
Sample Input
2
3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3
 
Sample Output
2
Computer
Math
English
3
Computer
English
Math

Hint

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)的更多相关文章

  1. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. HDU1074 Doing Homework —— 状压DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (J ...

  3. hdu_1074_Doing Homework(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:给你n个课程(n<=15)每个课程有限制的期限和完成该课程的时间,如果超出时间,每超 ...

  4. kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)

    Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7949   Accepted: 42 ...

  5. kuangbin专题十二 POJ1661 Help Jimmy (dp)

    Help Jimmy Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14214   Accepted: 4729 Descr ...

  6. kuangbin专题十二 HDU1176 免费馅饼 (dp)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. kuangbin专题十二 HDU1260 Tickets (dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. pa15-三省吾身

    序号 项 1 凡事提前10分钟    凡事提前10分钟,会让你有充裕的时间应对可能的突发事件,更加从容.    试着把起床闹钟提前10分钟,你就会发现你出门不必急匆匆,早饭也可慢慢享用,一整天的状态也 ...

  2. clang: error: linker command failed with exit code 1 (use -v to see invocation) 无法定位的问题

    编译出现错误:linker command failed with exit code 1 找到Build settings->Linking->Other Linker Flags,将此 ...

  3. dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

    dubbo-2.8.4需用jdk版本为1.8,dubbo-2.5.3可以使用1.7版本的jdk.

  4. Access restriction required library rt.jar

    在JAVA项目开发中,使用到了BASE64Decoder,但编辑运行时却会出现以下错误:Access restriction required library rt.jar,这里就详细的说明一下如何解 ...

  5. 关于android中,菜单按钮点击事件首次执行之后再次执行需要双击按钮的问题

    有时候在获取事件的时候,需要双击才能获取,解决方法很简单,把返回值设为true,那么这个事件就不会再分发了,我预计是设为其他值会继续分发,造成事件的相应混乱

  6. Using JConsole

    Using JConsole 转自 https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html Th ...

  7. 【Java基础专题】编码与乱码(05)---GBK与UTF-8之间的转换

    原文出自:http://www.blogjava.net/pengpenglin/archive/2010/02/22/313669.html 在很多论坛.网上经常有网友问" 为什么我使用 ...

  8. EZOJ #79

    传送门 分析 在经过若干次操作之后一定会产生一堆环 而我们又发现从一个点到另一个点实际可以经过所有环 于是问题就转换成了$k_1s_1 + k_2s_2 + ... + len = t$ 其中$s_i ...

  9. Luogu U15118 萨塔尼亚的期末考试(fail)

    感觉...昨天是真的傻... 题意 T个询问,每个询问给一个n,求 $ \frac{\sum_{n}^{i = 1}Fib_{i} * i}{n * (n + 1) / 2} $ Fib是斐波那契数列 ...

  10. 选择设置好ext3日志模式

    Linux是一种开放的.因Internet而产生的操作系统.Internet的发展.以网络为中心的计算模式如电子商务被迅速接受和普及,都为 Linux提供了更巨大的机会,使之成为企业和部门级的首选平台 ...