[kuangbin带你飞]专题十二 基础DP1 D - Doing Homework HDU - 1074

题目描述

 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.

题目的大意:现在有多门课程要学习,每门课程学习需要花费时间C,每门课程有deadline D,课程没有在deadline内学习完会扣分,晚一天,扣一分。题目的目的是求课程学习的顺序,让所扣分数最少。

思路

这道题使用状态压缩的dp。看着名字很唬人,但实际上状态压缩dp其实和普通的dp没有区别,只不过利用了计算机二进制存储的特点,对于状态只有两种的情况,状态压缩dp会省空间。算法思想上并没有什么不同,甚至会更简单,状态压缩dp一般会进行遍历,算法上会更简单。
首先解释一下什么是状态压缩。拿这道题来举例。假设有3门课程,“000”就代表3们课程都没有学习,“010”代表只有第二门课程学习了,“011”代表只有第一门课程没有学习。这些二进制的表示可以用十进制的数字来存储,存储空间就很小了,比如“000”其实就是0,“010”其实就是2,“011”其实就是3。我们用一个字节就可以表示。
接下来,我们讲这道题的思路。这道题属于动态规划,我们就要找状态转移方程。举个例子,当我们有3门课,每门课都完成的时候,状态是“111”,那这个状态可以由“101”,“011”,“110”三种状态转移而来,那么三门课都完成,被扣分数最少就从这三种状态中选一个,我们遍历这三种状态,选总扣分最小的就行。“101”,“011”,“110”这些状态的总扣分最少的计算也是通过遍历比较前面的状态得来的。
所以,我们只要从“000”开始由小到大的得到计算,就能得到”111“状态扣的最少总分。

AC代码

由于HDOJ的系统在维护,我的代码没有办法提交评测,这里就暂时不放代码了,等系统维护完成,我的代码提交通过了再放上来。毕竟样例过了,还可能会有一些边缘数据考虑不到,会出现WA:)

20201008更新AC代码

#include<iostream> 
#include <cstring>
#include <string> 
#define Inf 0x3f3f3f3f
using namespace std;
const int MAX_NUM = (1<<15)+10;
char s[20][101];
int dea[20],fea[20];
int dp[MAX_NUM],pre[MAX_NUM],t[MAX_NUM],mark[MAX_NUM]; 
void output_result(int num)
{
	if(num==0) return;
	output_result(pre[num]);
	cout<<s[mark[num]]<<endl;
}
int main()
{
	int T,N;
	int i,j;
	int score;
	int tmp;
	scanf("%d",&T);
	while(T--)
	{
		memset(dp,0,sizeof(dp));
		t[0] = 0;
		scanf("%d",&N);
		for(i = 0;i<N;i++) scanf("%s%d%d",&s[i],&dea[i],&fea[i]);

		for(i = 1;i<(1<<N);i++)
		{
			
			dp[i]=Inf;
			for(j = N-1;j>=0;j--)
			{
				tmp = (1<<j);
				if(!(tmp&i)) continue;
				score = t[i-tmp]+fea[j]-dea[j];
//				cout<<i<<" "<<j<<" "<<score<<endl; 
				if(score<0) score = 0;
				if((dp[i-tmp]+score)<dp[i])
				{
					dp[i] = dp[i-tmp]+score;
					mark[i] = j;
					pre[i] = i-tmp;
					t[i] = t[i-tmp]+fea[j];
				}
				
				
			}
		}
		cout<<dp[(1<<N)-1]<<endl;
		output_result((1<<N)-1);

		
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值