Description

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.

Sample Input

3
3 7 4
2 6 5
3 7 6

Sample Output

11
2
2 3

HINT

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.

题解

交这道题的时候,正好碰到$codeforces$被卡测评...等了(翻.墙逛P站逛了)好久...

首先我们考虑这样一个问题,我们救物品如果没有烧毁时间,无论先救什么都可以,但既然有烧毁时间,那是不是越先烧毁的就越需要先救。

我们按$d$值从小到大排序(这种基于排序的背包之前做过一道:[TJOI 2013]拯救小矮人。至于为什么要排序,思路差不多,不懂的可以戳回去看看),然后做朴素的背包。

由于要输出路径,$dp$数组我们必须多开一维,令$f[i][j]$表示选到排完序后的$i$号物品时结束时间为$j$的可能的最大价值:

$f[i][j] = Max(f[i-1][j], f[i-1][j-a[i].t]+a[i].p)$,注意边界情况。

另外特别要注意的是,对于$d$的解释:即若救$i$号物品,那么一定要在时刻$d_i$之前完成。

 //It is made by Awson on 2017.9.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} struct item {
int t, d, p, id;
bool operator < (const item &b) const{
return d < b.d;
}
}a[];
int n, maxd;
int f[][];
int pre[][]; void print(int i, int j, int cnt) {
if (i == ) {
printf("%d\n", cnt);
return;
}
if (pre[i][j] == j) print(i-, j, cnt);
else {
print(i-, pre[i][j], cnt+);
printf("%d ", a[i].id);
}
}
void work() {
read(n);
for (int i = ; i <= n; i++) {
read(a[i].t), read(a[i].d), read(a[i].p);
a[i].id = i;
maxd = Max(maxd, a[i].d);
}
sort(a+, a++n);
for (int i = ; i <= n; i++)
for (int j = ; j <= maxd; j++) {
f[i][j] = f[i-][j];
pre[i][j] = j;
if (j >= a[i].t && j < a[i].d)
if (f[i-][j-a[i].t]+a[i].p > f[i][j]) {
f[i][j] = f[i-][j-a[i].t]+a[i].p;
pre[i][j] = j-a[i].t;
}
}
int loc = , ans = ;
for (int i = ; i < maxd; i++) {
if (f[n][i] > ans) {
ans = f[n][i];
loc = i;
}
}
printf("%d\n", ans);
print(n, loc, );
}
int main() {
work();
return ;
}

[Codeforces 864E]Fire的更多相关文章

  1. Codeforces 864E Fire(DP)

    题目链接 Fire 题意 有n个物品,每个物品的挽救时间代价为ti, 消失时刻为di, 价值为pi. 如果要救某个物品,必须在他消失之前救出来. 同一时刻最多只能救一件物品. 当前耗时为当前已经救出的 ...

  2. Codeforces 864E - Fire(dp)

    原题连接:http://codeforces.com/problemset/problem/864/E 题意:一个人想从大火中带走一些东西.每次他只能带一个,耗时ti ,价值为pi, 当总时间超过di ...

  3. Codeforces 864E Fire(背包DP)

    背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...

  4. H - Fire CodeForces - 864E 01背包

    https://codeforces.com/problemset/problem/864/E 这个题目要把这个按照物品毁灭时间进行排序,如果时间短就要排在前面,这个是因为要保证之后的物品的拯救不会影 ...

  5. Codeforces 864E dp

    题意: 房间着火了,里面有n件物品,每件物品有营救需要的时间t,被烧坏的最晚时间d,他的价值p,问能得到的最大价值,并且输出营救出来的物品编号 代码: //必然是先救存活时间短的即d小的,所以先排个序 ...

  6. codeforce 35C fire again

    2017-08-25 17:04:07 writer:pprp 题目描述: • Codeforces 35C Fire Again• N*M的格子,最开始有K个点 (坐标给定) 开始着火• 每一秒着火 ...

  7. D. Frets On Fire 【二分,前缀和】 (Codeforces Global Round 2)

    题目传送门:http://codeforces.com/contest/1119/problem/D D. Frets On Fire time limit per test 1.5 seconds ...

  8. Codeforces Round #436 (Div. 2) E. Fire

    http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...

  9. Fire Again CodeForces - 35C (BFS)

    After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows ...

随机推荐

  1. 简单的C语言编译器--语义制导翻译

      语法分析是最难写的,而这部分确实最伤脑的.大量的语义动作分析差点把我逼疯.   简而言之,这部分的作用就是在每次归约之后,都进行一些语义动作,最终让我们得到测试程序的三地址码,即中间代码. 1. ...

  2. "未找到应用程序的“aps-environment”的权利字符串"

    1.先生成App ID,在去Provisioning里面生成新的Profile 2.删除Xcode里面原来的push profile(如果没有就不用删除)再次双击新下载的profile(mobilep ...

  3. iOS中滤镜种类及相关介绍

  4. Linux 帳號管理與 ACL 權限設定

    1. Linux 的账号与群组1.1 使用者识别: UID 与 GID1.2 使用者账号:/etc/passwd, /etc/shadow1.3 关于群组: 有效与初始群组. groups, newg ...

  5. nyoj n-1位数

    n-1位数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的 ...

  6. Python-字典、集合、字符编码、文件操作整理-Day3

    1.字典 1.1.为什么有字典: 有个需求,存所有人的信息 这时候列表就不能轻易的表示完全names = ['stone','liang'] 1.2.元组: 定义符号()t = (1,2,3)tupl ...

  7. 租户、租户管理员、部门管理员和开发者在APIGW中的角色

    一.参与者 1.vdcId:租户 2.运营管理员 operator: 一种角色 创建开发商 审批外置服务,如:hadoop集群 审批内置服务,如:<API使用申请> 3.租户管理员     ...

  8. io使用的设计模式

    File f = new File("c:/a.txt"); 1. FileInputStream fis = new FileInputStream(f); 2. Reader ...

  9. java中的引用类型的对象存放在哪里

    根据上下文来确定.比如void func(){    Object obj = new Object();//这个obj在函数的栈里.}class Test{   private Object obj ...

  10. linux下mongodb安装、服务器、客户端、备份、账户命令

    在linux环境安装mongoDB: 一般认为偶数版本为稳定版 如 1.6.x,奇数版本为开发版如1.7.x 32bit的mongoDB最大能存放2g的数据,64bit没有限制 方法1: 终端执行: ...