kuangbin专题十二 HDU1074 Doing Homework

传送门http://acm.hdu.edu.cn/showproblem.php?pid=1074

题目大意:给出一系列的作业,还有每一门作业的截止日期和做完需要的时间,老师规定每超过一天,就要扣一分,让你求一个做作业的顺序,使最后扣分最少。如果扣分相同,输出字典序最小的序列。

思路:本来以为只是贪心,但是发现没有解释的过的策略。然后搜了题解,发现是状压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的博客。

AC代码:

#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-5;
const ll mod = 1e9+7;

struct node{
	string name;
	int end, cost;
}stu[20];//初始的 

struct Node{
	int time, now, pre, score;
}dp[1<<15]; 

int main() {
	int _, n;
	for(scanf("%d", &_);_;_--) {
		scanf("%d", &n);
		forn(i, 0, n) {
			cin >> stu[i].name >> stu[i].end >> stu[i].cost;
		}
		forn(s, 1, (1<<n)) {//全排列所有状态 
			dp[s].score = inf;//刚开始全是正无穷 
			nfor(i, n, 0) {
				int temp = 1 << 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)//小于0,就不用减 
					st = 0;
				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 = (1<<n)-1;
		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=(1<<15)+10;
int n;
int dp[MAX],t[MAX],pre[MAX],dea[20],fin[20];//dp[i]记录到达状态i扣的最少分,t时相应的花去多少天了
char s[20][110];

void output(int x){
	if(!x)return;
	output(x-(1<<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的前驱,为了最后输出完成作业的顺序 
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值