题目:
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 tiseconds 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.
Examples
input
3 3 7 4 2 6 5 3 7 6
output
11 2 2 3
input
2 5 6 1 3 3 5
output
1 1 1
Note
In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2seconds. Thus, the total value of the saved items will be 6 + 5 = 11.
In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3seconds, but this item will already be completely burned by this time.
题意分析:01背包+输出选择(具体解释在代码里,可以复制过去查看)
代码:
#include<bits/stdc++.h>
using namespace std;
struct ljh
{
int t,d,p,num;//救援需要的时间,截止时间,价值,编号
}e[105];
int n,ans=0,k=0;
int f[2020],sum[105];//f[i]表示第i天所获得的最大利益,sum用来储存在最大利益下被选择的物品编号
bool vis[105][2020];//第i件物品,在第j天是否被用
bool cmp(ljh a,ljh b)
{
return a.d<b.d;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&e[i].t,&e[i].d,&e[i].p);
e[i].num=i;
}
sort(e+1,e+n+1,cmp);
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
{
int nowt=e[i].t;
for(int j=e[i].d-1;j>=nowt;j--)
{
if(f[j]<f[j-nowt]+e[i].p)
{
f[j]=f[j-nowt]+e[i].p;
vis[i][j]=1;
//表示当前的第j天,我的当前第i件物品是需要被使用的
}
}
}
for(int i=1;i<=e[n].d;i++)
if(f[i]>f[ans])ans=i;
printf("%d\n",f[ans]);
for(int i=n;i>=1;i--)
{
if(vis[i][ans]==1)
//这个地方很多人会有坑点就是疑惑,我在第j天有物品被使用,但是可能在之前的更新中有物品同样的在第j天被使用过,而后又被替换了,那怎么办
//其实这个都不是问题,因为我在前面更新第j天使用的物品的时候,物品是从1~n开始逐一查看是否被使用的,而我这里的for循环遍历天数也正好是从最后一天开始往前面找的
//如果我后面第j天,有新的物品被利用,那么这个新的物品根据for(i:1~n)肯定是在后面被查找的
//那么也许还会有疑惑,如果我那个相对于第j天有一个价值小的物品在后面怎么办,实际上是不用担心的,因为这时候价值大的在前面被利用了,价值小的在后面1就不会被更新了,那么这时候的vis是等于0的
{
k++;
sum[k]=e[i].num;//记录路劲
ans-=e[i].t;//因为我物品是for(1~n)的,那么我前面的物品肯定先被利用,那么当前剩下的天数肯定是选取的前面的物品了
}
}
printf("%d\n",k);
for(int i=k;i>=1;i--)
printf("%d ",sum[i]);
return 0;
}