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]表 ...
随机推荐
- python数据结构与算法第十一天【希尔排序】
1.希尔排序的原理 2.代码实现 def shell_sort(alist): n = len(alist) # 初始步长 gap = n / 2 while gap > 0: # 按步长进行插 ...
- 老男孩python学习自修【第二天】字符串用法
实时处理增量日志最佳实践 主要使用f.seek()和f.tell()实现 字符串处理函数: s.find(substr, start, end) 查找子字符串,找不到则返回-1,找到则返回对应的索引 ...
- git放弃修改,强制覆盖本地代码
$ git fetch --all $ git reset --hard origin/master $ git pull
- 【python练习题】程序4
# 题目:输入某年某月某日,判断这一天是这一年的第几天? import time year = input('输入年份: \n') month = input('输入月份: \n') day = in ...
- vpx
VPX 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! VPX总线是VITA(VME International Trade Association, VME国际贸易协 ...
- .net core compatibility windows & windows compatible Linux
Who is this package for? This package is meant for developers that need to port existing .NET Framew ...
- 百年老图难倒谷歌AI,兔还是鸭?这是个问题
上面这张图,画的是鸭子还是兔子? 自从1892年首次出现在一本德国杂志上之后,这张图就一直持续引发争议.有些人只能看到一只兔子,有些人只能看到一只鸭子,有些人两个都能看出来. 心理学家用这张图证明了一 ...
- Jquery根据滚动条显示返回按钮
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- winFormToMysql&&几个控件的数据绑定
运行图: 代码: private void button1_Click(object sender, EventArgs e) { string str = "database=csharp ...
- 炎黄流程中改流程节点颜色的js