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. libstdc++.so.6

    libstdc++.so.6遇到的问题: 1.提示version `GLIBCXX_3.4.14' not found /usr/lib64/libstdc++.so.: version `GLIBC ...

  2. Linux系统下Oracle执行SQL脚本后中文出现乱码解决方法

    先确认Oracle的字符集,sqlplus登录Oracle后执行语句:   [sql] select userenv('language') from dual; 返回值例如:AMERICAN_AME ...

  3. GridControl 隐藏Drag a column header here to group by that column

    解决方案: 打开设计器,找到OptionsView,往下拉设置showGroupPanel为false 

  4. 下载Django

    Django下载教程以及学习教程https://code.ziqiangxuetang.com/django/django-queryset-api.html 或者直接搜索自强学堂

  5. createPlaceholder 函数

    function createPlaceholder(id,text,top,left){ top = top || 0; left = left || 0; var obj = $('#' + id ...

  6. Android中pull解析XML文件的简单使用

    首先,android中解析XML文件有三种方式,dom,sax,pull 这里先讲pull,稍候会说SAX和DOM pull是一种事件驱动的xml解析方式,不需要解析整个文档,返回的值是数值型,是推荐 ...

  7. 全文检索技术---Lucene

    1       Lucene介绍 1.1   什么是Lucene Lucene是apache下的一个开源的全文检索引擎工具包.它为软件开发人员提供一个简单易用的工具包(类库),以方便的在目标系统中实现 ...

  8. HDU 6397(2018多校第8场1001) Character Encoding 容斥

    听了杜教的直播后知道了怎么做,有两种方法,一种构造函数(现在太菜了,听不懂,以后再补),一种容斥原理. 知识补充1:若x1,x2,.....xn均大于等于0,则x1+x2+...+xn=k的方案数是C ...

  9. DOM 操作属性

    DOM操作就是针对对象的操作 先写一个按钮,<input tupe="button" value=""  id="id">  这 ...

  10. Matlab 摄像机标定+畸变校正

    博客转载自:http://blog.csdn.net/Loser__Wang/article/details/51811347 本文目的在于记录如何使用MATLAB做摄像机标定,并通过opencv进行 ...