Doing Homework HDU - 1074 (状压dp)
InputThe 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.
OutputFor 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. 题意:
有n个作业,每个作业有一个截止日期和一个完成所需时间,如果一个作业迟交x天,就要扣除x个学分,求扣除作业最小的写作业顺序,如果多个方案,输出字典序最小的那个。
思路:
状压dp
dp[i][j]:j是状压后的数字,二进制位上,1表示以完成,0表示未完成。i表示i是j状态最后一个完成的作业。
用一个pre数组记录完成作业的顺序。
状态转移方程请详见代码。
此题的要点是在j状态下,dp数组增加一维i来记录最后一个完成的作业,用以记录作业完成的顺序。
当然其他博主有一维的写法,不过私以为我的代码更容易理解,不过时间复杂度也多了一个n。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int n;
char name[][];
int d[],c[];
int dp[][];
int f[];
struct node{
int x,y;
};
node pre[][];
stack<int>st;
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s%d%d",name[i],&d[i],&c[i]);
}
memset(dp,0x3f,sizeof(dp));
memset(f,,sizeof(f));
memset(pre,-,sizeof(pre)); dp[][]=;
int m=(<<n)-;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(i&(<<j)){continue;}
int k=i|(<<j);
f[k]=f[i]+c[j];
for(int t=;t<n;t++){
if(dp[j][k]>=dp[t][i]+max(,f[k]-d[j])){
dp[j][k]=dp[t][i]+max(,f[k]-d[j]);
pre[j][k]=node{t,i};
}
}
}
}
int ans=inf,s;
for(int i=;i<n;i++){
if(ans>=dp[i][m]){ans=dp[i][m];s=i;}
}
printf("%d\n",ans);
node exa=node{s,m};
while(true){
if(exa.y==){break;}
st.push(exa.x);
exa=pre[exa.x][exa.y];
}
while(!st.empty()){
printf("%s\n",name[st.top()]);
st.pop();
}
}
return ;
}
Doing Homework HDU - 1074 (状压dp)的更多相关文章
- D - Doing Homework HDU - 1074 (状压dp)
题目链接:https://cn.vjudge.net/contest/68966#problem/D 具体思路:我们可以把每个情况都枚举出来,然后用递归的形式求出最终的情况. 比如说 我们要求 10 ...
- Doing Homework(HDU 1074状压dp)
题意:给你n个要做的作业,它们的名字.期限.可完成所需天数(必须连续)在规定期限不能完成要扣分(每天一分)求做作业顺序使扣分最少. 分析:作业数量较少,用状态压缩,做到第i种作业花费的天数dp[i]. ...
- HDU 1074状压DP
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1074 (状压dp)
题意: 给出几个学科的作业.每个作业剩余的时间.完成每个学科作业的时间.如果在剩余时间内不能完成相应作业 就要扣分 延迟一天扣一分 求最小扣分 解析: 把这些作业进行全排列 求出最小扣分即可 但A( ...
- HDU 4778 状压DP
一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...
- HDU 3001 状压DP
有道状压题用了搜索被队友骂还能不能好好训练了,, hdu 3001 经典的状压dp 大概题意..有n个城市 m个道路 成了一个有向图.n<=10: 然后这个人想去旅行.有个超人开始可以把他扔到 ...
- hdu 2809(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 思路:简单的状压dp,看代码会更明白. #include<iostream> #in ...
- hdu 2167(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2167 思路:经典的状压dp题,前后,上下,对角8个位置不能取,状态压缩枚举即可所有情况,递推关系是为d ...
- Engineer Assignment HDU - 6006 状压dp
http://acm.split.hdu.edu.cn/showproblem.php?pid=6006 比赛的时候写了一个暴力,存暴力,过了,还46ms 那个暴力的思路是,预处理can[i][j]表 ...
随机推荐
- css瀏覽器私有前綴名
-webkit-:chrome,safari -o-:opera -moz-:firefox -ms-:ie
- 二、kubernetes
一.kubernetes(简称k8s) 集群示意图 Kubernetes工作模式server-client,Kubenetes Master提供集中化管理Minions.部署1台Kubernetes ...
- Codeforces#543 div2 B. Mike and Children(暴力?)
题目链接:http://codeforces.com/problemset/problem/1121/B 题意 给n个数 最多的对数 其中每一对(i,j)的ai+aj都相等(不知道怎么解释.... 判 ...
- Python读取excel中的图片
作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...
- Nginx grpc反向代理
L111 首先Grpc 默认编译进Nginx 但是依赖http_v2模块 需要编译进nginx 具体指令可以参考Nginx http 反向代理 指令都类似 分布式反向代理 server { serve ...
- WMS工作原理
图1:创建窗口 图2:通信过程 我们知道其实任何一个窗口的创建,最终都是会创建一个 ViewRootImpl对象.ViewRootImpl 是一很重要的类,类似 ActivityThread 负责跟A ...
- 基于jQuery-ui实现多滑块slider
效果图: 代码: <!doctype html> <html lang="en"> <head> <meta charset=" ...
- 使用Java泛型返回动态类型
返回一个指定类型的集合,并且clazz必须继承IGeoLog对象或者是其本身 <T extends IGeoLog> List<T> getLogListSql(Class&l ...
- webpack——快速入门【一】
学习webpack https://github.com/webproblem/learning-article#webpack https://github.com/lengziyu/learn-w ...
- Fence Repair POJ - 3253 哈夫曼思想 优先队列
题意:给出一段无限长的棍子,切一刀需要的代价是棍子的总长,例如21切一刀 变成什么长度 都是代价21 列如7切成5 和2 也是代价7题解:可以利用霍夫曼编码的思想 短的棍子就放在底层 长的尽量切少一次 ...