Codeforces 864E (Codeforces Round #436 (Div. 2)) E. Fire 背包输出路径
来源:互联网 发布:淘宝店铺618活动策划 编辑:程序博客网 时间:2024/06/10 14:00
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 + tbseconds 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.
33 7 42 6 53 7 6
1122 3
25 6 13 3 5
111
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 2 seconds. 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 3 seconds, but this item will already be completely burned by this time.
失火了,有n个物品,每个物品有价值pi,必须在时间di前被救,否则就要被烧毁。救某个物品需要时间ti,问最多救回多少价值的物品,并输出救物品的顺序。
要使总价值最高,很容易发现这是一个01背包的DP,把时间当做代价,按di排序,优先考虑先被烧毁的物品,就好了。
输出路径的话,因为N比较小,可以直接用状态压缩记录某件物品有没有被取。100个物品,压成两个数就可以了。
因为DP时是按照di的大小排序来考虑的,所以得到最优价值所对应的取法之后,把这些物品按照di再次排序就是救物品的顺序了。
#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <deque>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=2005, inf = 0x3f3f3f3f;const ll llinf = 0x3f3f3f3f3f3f3f3f;const ld pi = acos(-1.0L);int dp[maxn], cnt[maxn];ll b[105];ll con[maxn][2];struct khalid {int t, d, val, id;};khalid a[105],p[105];bool cmp(khalid a, khalid b) {return a.d < b.d ;}int main() {mem0(dp); mem0(cnt);mem0(con);int n, i, j, ans, k;stack<int> st;scanf("%d", &n);for (i = 1; i <= n; i++) {scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].val);a[i].id = i;}b[0] = 1;for (i = 1; i <= 51; i++) {b[i] = b[i - 1] * (ll)2;}sort(a + 1, a + n + 1, cmp);for (i = 1; i <= n; i++) {for (j = a[i].d-1; j >= a[i].t; j--) {if (dp[j] < dp[j - a[i].t] + a[i].val) {dp[j] = dp[j - a[i].t] + a[i].val;cnt[j] = cnt[j - a[i].t] + 1;if (i <= 50) {con[j][0] = con[j - a[i].t][0] ^ b[i-1];con[j][1] = con[j - a[i].t][1];}else {con[j][1] = con[j - a[i].t][1] ^ b[i-51];con[j][0] = con[j - a[i].t][0];}}}}ans = 0;for (i = 0; i <= 2000; i++) {if (dp[i] > dp[ans]) ans = i;}printf("%d\n%d\n",dp[ans],cnt[ans]);int m = 0;//printf("%lld %lld\n", con[ans][0], con[ans][1]);for (i = 1; i <= 50; i++) {if ((con[ans][0] >> i - 1) % 2 == 1) p[++m].id = a[i].id,p[m].d=a[i].d;if ((con[ans][1] >> i - 1) % 2 == 1) p[++m].id = a[50+i].id, p[m].d = a[i+50].d;}sort(p + 1, p + m + 1, cmp);for (i = 1; i <= m; i++) {printf("%d ", p[i].id);}//system("pause");return 0;}
- Codeforces 864E (Codeforces Round #436 (Div. 2)) E. Fire 背包输出路径
- Codeforces Round #436 (Div. 2)-E-Fire(01背包输出路径)
- Codeforces Round #436 (Div. 2) E. Fire(背包DP+输出路径)
- Codeforces Round #436 (Div. 2) E. Fire(01背包+输出路径)
- Codeforces Round #436 E.Fire(01背包 + 输出路径)
- 【01背包 && 记录路径 && 约束】Codeforces Round #436 (Div. 2) E. Fire
- Codeforces Round #436 (Div. 2)-背包&排序&输出状态-E. Fire
- Codeforces Round #436 (Div. 2) E. Fire
- Codeforces Round #436 (Div. 2)E.Fire
- Codeforces Round #436 (Div. 2)E. Fire
- Codeforces Round #436 (Div. 2) E. Fire
- Codeforces Round #436 (Div. 2) 864E. Fire
- Codeforces Round #436 (Div. 2) E. Fire (有放入顺序有关的01背包)
- Codeforces Round #436 (Div. 2) E fire DP
- Codeforces Round #436 E. Fire
- Codeforces 864E Fire【排序+背包dp】
- Codeforces 864E Fire(01背包)
- Codeforces Round #436 (Div. 2) E
- ViewPager+RecyclerView联动
- 异常和断言2
- Java基础之动态编译
- Oracle客户端使用
- Android中集成讯飞语音,语音转文字以及文字转语音操作
- Codeforces 864E (Codeforces Round #436 (Div. 2)) E. Fire 背包输出路径
- STM8 如何自制BootLoader并实现在线升级
- bootstrap后台 uniform.default.css 使用checkbox 默认选不中问题
- Spring4.x:Spring Aware
- web项目:漏洞修复(3)_spring过滤器(1)
- 【WEB】JS面试题涉及基础知识总结(二)
- 实现ViewPager动态添加和删除页面
- android 问题整理
- JAVA基础语法