Codeforces 864E (Codeforces Round #436 (Div. 2)) E. Fire 背包输出路径

来源:互联网 发布:淘宝店铺618活动策划 编辑:程序博客网 时间:2024/06/10 14:00

E. Fire
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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 ≤ 201 ≤ di ≤ 2 0001 ≤ 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
33 7 42 6 53 7 6
output
1122 3 
input
25 6 13 3 5
output
111 
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 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;}

阅读全文
0 0
原创粉丝点击