codeforces——contest 864 problemE
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.
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.
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.
3
3 7 4
2 6 5
3 7 6
11
2
2 3
2
5 6 1
3 3 5
1
1
1
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.
————————————————————————————
这道题其实就是有人家着火了 一个物品有三个属性 营救所需要的时间ti 必须在di前被救(注意是前) 价值为 pi
要求输出最多能救的最大价值 并输出方案 即营救了多少物品 并输出这些物品 (按营救顺序
这样之后呢 我们发现最大价值其实可以背包 而背包的方案输出可以用一个数组记一下转移顺序就可以了
#include<cstdio> #include<cstring> #include<algorithm> using std::max; using std::sort; const int M=107; int read(){ int ans=0,f=1,c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();} return ans*f; } int n,f[M][2007],map[M][2007]; struct pos{int t,d,p,id;}q[M]; bool cmp(pos a,pos b){return a.d<b.d;} int qu[M],cnt; int mx=0,id; int main(){ n=read(); for(int i=1;i<=n;i++) q[i].t=read(),q[i].d=read()-1,q[i].p=read(),q[i].id=i; std::sort(q+1,q+1+n,cmp); for(int i=1;i<=n;i++){ for(int j=q[i].d;j>=q[i].t;j--){ if(f[i-1][j-q[i].t]+q[i].p>f[i-1][j]){ f[i][j]=f[i-1][j-q[i].t]+q[i].p; map[i][j]=j-q[i].t; } else{f[i][j]=f[i-1][j]; map[i][j]=j;} } for(int j=q[i].t-1;j;j--) f[i][j]=f[i-1][j],map[i][j]=j; } for(int j=1;j<=q[n].d;j++) if(f[n][j]>mx) mx=f[n][j],id=j; printf("%d\n",mx); int l=n,r=id; for (int i=n;i>=1;i--){ if (map[i][r]<r) qu[++cnt]=q[i].id; r=map[i][r]; } printf("%d\n",cnt); for(int i=cnt;i;i--) printf("%d ",qu[i]); return 0; }