CodeForces - 864E (加限制条件的背包)

Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.

Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tb seconds after fire started.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house.

Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.

Output

In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.

 

一个dp题,时间为容量,抢救时间为花费,可以用滚动数组优化到一维。先拿二维的为例,dp[i][j]表示在第j秒抢救前i个物品所能得到的最大价值,注意该题要对每个物品的死亡时间从小到大排序。

还有一个难点就是记录物品了,我们可以用一个vector数组num, 具体用法看代码

下面是滚动数组的代码

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
#define LL long long
const int MAX = 2000;

struct date
{
	int t;
	int d;
	int p;
	int n; // n记录商品编号,因为排序会打乱顺序
} a[105];

bool cmp(date A, date B){
	return A.d < B.d; // 按死亡时间排序
}

int dp[MAX];
vector<int> num[MAX];

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p);
		a[i].n = i;
	}

	sort(a + 1, a + n + 1, cmp);
	for(int i = 1; i <= n; i++){
		for(int j = MAX; j >= a[i].t; j--){
			if(j < a[i].d){ // j是完成抢救的时间,所以必须小于死亡时间
				if(dp[j] < dp[j - a[i].t] + a[i].p){ 
					dp[j] = dp[j - a[i].t] + a[i].p;
					num[j] = num[j - a[i].t]; // 关键步骤,因为dp[j]是由dp[j - a[i].t] + a[i].p转移过来,所以要将num[j]变为num[j - a[i].t]
					num[j].push_back(a[i].n); // 再将该物品的编号放入
				}
			} 
		}
	}
	int ans = -1;
	int fg = 0;
	for(int i = 0; i <= MAX; i++){ // 找到最大的情况,记录对应的背包容量
		if(dp[i] > ans){
			ans = dp[i];
			fg = i;
		}
	}

	printf("%d\n", ans);
	printf("%d\n", num[fg].size());
	int len = num[fg].size();
	for(int i = 0; i < len; i++){
		printf("%d ", num[fg][i]);
	}
	printf("\n");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值