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. Ubuntu登录异常: 输入正确的密码, 但是却无法进入系统, 总是返回到登录界面, 但是用ctrl+alt+F1-F文字界面登录都可以进入。

    今天打开电脑的时候, 在输入密码之后, 未进入ubuntu的桌面, 而是显示了几行英文之后有返回到了登录界面.显示的英文如下: could not write bytes: Broken pipe   ...

  2. python读取配置文件 ConfigParser

    Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...

  3. tomcat 三种部署方式以及server.xml文件的几个属性详解

    一.直接将web项目文件件拷贝到webapps目录中 这是最常用的方式,Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.如果你想要修改这个默认 ...

  4. Shell编程进阶 1.6 if判断的几种用法

    针对文件和目录的逻辑判断 touch .txt .txt ]; then echo ok;fi -f 判断1.txt是否是文件且是否存在,成立输出ok if [-d /tmp/ ]; then ech ...

  5. mui封装的ajax请求

    由于项目中引进MUI框架,所以就不需要引进jquery,但需要和后台交互时,常写为jquery格式:所以笔者觉得有必要将mui封装的ajax请求在这里提一下: 1,mui框架基于htm5plus的XM ...

  6. C++深度解析教程学习笔记(1)C到C++的升级

    1.现代软件产品架构图 比如商场收银系统 2.C 到 C++ 的升级 2.1变量的定义 C++中所有的变量都可以在需要使用时再定义,而 C 语言中的变量都必须在作用域开始位置定义. 2.2 regis ...

  7. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-008Polymorphic many-to-one associations(@ManyToOne、@Inheritance、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.associations.manytoone; import org.jpwh.model.Consta ...

  8. bzoj5450 轰炸

    传送门 分析 不难想到如果这个图是一个DAG则答案就是图的最长路 于是我们考虑有环的情况 我们发现一个环上的所有点颜色一定不相同 于是我们发现答案就是缩点之后跑一遍点权最长路 点权就是这个强联通分量中 ...

  9. netty中的PoolChunk

    数据结构学的烂,看这个类比较的吃力 PoolChunk主要使用long allocate(int normCapacity) 在buffer pool中分配buffer.这个类有几个重要的概念:pag ...

  10. Win提权思路,方法,工具(小总结)[转]

    Win提权思路,方法,工具(小总结)[转] 看到这个文章,感觉整理的不错,就收藏下了. 介绍 windows提权总是被归结为适当的枚举.但要完成适当的枚举,你需要知道要检查和查找的内容.这通常需要伴随 ...