PAT(Top Level) 1002

动态规划——01背包

链接: https://pintia.cn/problem-sets/994805148990160896/problems/994805156145643520

1002 Business (35 分)
As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given 3 projects as the following:
Project[1] takes 3 days, it must be finished in 3 days in order to gain 6 units of profit. Project[2] takes 2 days, it must be finished in 2 days in order to gain 3 units of profit. Project[3] takes 1 day only, it must be finished in 3 days in order to gain 4 units of profit.
You may take Project[1] to gain 6 units of profit. But if you take Project[2] first, then you will have 1 day left to complete Project[3] just in time, and hence gain 7 units of profit in total. Notice that once you decide to work on a project, you have to do it from beginning to the end without any interruption.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤50), and then followed by N lines of projects, each contains three numbers P, L, and D where P is the profit, L the lasting days of the project, and D the deadline. It is guaranteed that L is never more than D, and all the numbers are non-negative integers.
Output Specification:
For each test case, output in a line the maximum profit you can gain.

Sample Input:
4
7 1 3
10 2 3
6 1 2
5 1 1
Sample Output:
18

这是一道动态规划01背包问题。动态规划和分治法类似,不同的就是它具有记忆性,它记录了所有已经解决的子问题答案,然后在新问题里就可以直接利用,避免了重复计算。
我们如果把任务作为物品i,0/1代表是否做了,时间就是背包,而任务的持续时间就是物品的“重量”,判断条件是:当前时间 + 该任务的持续时间 <= 该任务的deadline。这是一个填表的过程,时间复杂度为O(背包容量*物品个数)。还可以进行空间优化,因为V(i, j)只与V(i-1, j)相关,那么只需要一个一维数组,从后往前(一定要注意capacity从N…0)这样就行了。不过这样虽然优化了空间,只是不能直接找到最优解的组成了。
这里有个好文链接推荐:

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<math.h>
using namespace std;

struct project {
	int profit, last, ddl;
	void input() { scanf("%d %d %d", &profit, &last, &ddl); }
	bool operator<(project &p) { return ddl < p.ddl; }
};
int main()
{
	int proNum;
	scanf("%d", &proNum);
	vector<project> vp(proNum);
	for (int i = 0; i < proNum; i++) {
		vp[i].input();
	}
	sort(vp.begin(), vp.end());

	map<int, int> vprofit[52];//相当于是52行的表格
	vprofit[0][0] = 0;
	int maxres= 0 ;
	for (int i = 0; i < proNum; i++)
	{
		vprofit[i + 1] = vprofit[i];//复制一整行
		std::map<int, int>::iterator it;
		for (it = vprofit[i].begin(); it != vprofit[i].end(); it++)
		{
			if (it->first + vp[i].last <= vp[i].ddl)
			{
				vprofit[i + 1][it->first + vp[i].last] = max(vprofit[i + 1][it->first + vp[i].last], it->second + vp[i].profit);
				maxres = max(maxres, vprofit[i + 1][it->first + vp[i].last]);
			}
		}
	}
	cout << maxres;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值