pat-top 1002. Business (35)

https://www.patest.cn/contests/pat-t-practise/1002


有条件的动态规划:先要排序,保证deadline靠前的task先考略,否则可能会被排在前面的deadline晚但是profit大的task 淹没而不能被考虑。


#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct task {
	int p, l, d;
	task(int _p, int _l, int _d) {
		p = _p;
		l = _l;
		d = _d;
	}
	friend bool operator < (struct task a, struct task b) {
		return a.d < b.d;
	}
}Task;

int main()
{
	int n, p, l, d,maxD=0;
	scanf("%d", &n);
	vector<Task> tasks;
	
	for (int i = 0; i < n; i++)
	{
		scanf("%d %d %d", &p, &l, &d);
		if (maxD < d) maxD = d;
		Task t(p, l, d);
		tasks.push_back(t);
	}
	/** Here we have to sort to make sure task with early deadline could be consider,
	*	since task with later deadline, could be reconsider later.
	*	For example, t[0] p:7,l:1,d:2, t[1]:p:5,l:1,d:1
	*   If not sort, first iteration dp[1-2] = {7,7}, seconde iteration: dp[1-2] ={7,7}
	*	If sort first iteration dp[1-2] = {5,5}, second iteration: dp[1-2] = {7,12}
	*/
	sort(tasks.begin(), tasks.end());
	
	int *dp = new int[maxD+1];
	fill(dp, dp + maxD + 1, 0);
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = maxD; j >= tasks[i].l; j--)
		{
			if (j <= tasks[i].d) {
				dp[j] = max(dp[j], dp[j - tasks[i].l] + tasks[i].p);
				if (dp[j] > ans) ans = dp[j];
			}
		}
	}
	printf("%d\n", ans);
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值