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. 学号:201621123032 《Java程序设计》第1周学习总结

    1:本周学习总结 JDK,JRE,JVM三者的含义和关系.JDK是java开发工具包,包含了java的运行环境,java工具和类文库.例如java.javac.jar....可以把 .java编译成. ...

  2. map的infowindow的show事件(ArcGIS API for JS)

  3. 我从业11年来遇到的最奇葩的raid0+1数据恢复经历

    我是一名数据恢复工程师,从事数据恢复行业已经11年了,前几天接到一组4块盘SCSI RAID0+1的数据恢复,客户说做了两组raid1,现在raid状态里显示有3快盘offline.如果两组盘分别作r ...

  4. Python之旅_计算机基础入门

    一.计算机基础 1.Python是编程语言 语言:一种事物与另一种事物沟通的介质. 编程语言:程序员与计算机沟通的介质. 什么是编程:程序员用编程语言把自己的逻辑思想下来,编程的结果就是一堆文件. 为 ...

  5. Mego(04) - Mego入门

    本教程演示创建一个简单的数据库访问及更新数据的示例以便于初步了解下Mego框架的使用. 文中使用Visual Studio 2017版本. 创建Visual Studio项目 创建一个名为 MegoS ...

  6. Web Api 使用模型验证

    public class Person { public int Id { get; set; } [Required(ErrorMessage = "姓名不能为空啊啊啊!")] ...

  7. "双非"应届生校招如何获得大厂青睐?(内附技术岗超全求职攻略)

    写在前面的话 笔者从17年的2月份开始准备春招,其中遇到不少坑,也意识到自己走过的弯路.故写了这篇文章总结一番,本文适合主动学习的,对自己要学的课程不明确的,对面试有恐惧症的...等将来打算从事技术岗 ...

  8. kubernetes入门(02)kubernetes的架构

    一.kubernetes的主从架构 kubectl,全称 Kubernetes Control Plane,它表示Kubernetes为了实现最终的目标而构建的一组集群范围内的进程,这组进程相互协调, ...

  9. SpringCloud的服务注册中心(一)

    一.概念和定义 1.服务治理:服务注册与服务发现 服务注册中心,提供服务治理功能,用来实现各个微服务实例的自动注册与发现. 服务注册与发现对于微服务系统来说非常重要.有了服务发现与注册,维护人员就不需 ...

  10. Python之递归函数

    递归函数 初识递归函数 递归函数的定义:在一个函数里再调用这个函数本身 Python为了考虑保护内存占用情况,有一个递归深度的限制. 探究递归的默认最大深度: def foo(n): print(n) ...