[Codeforces 864E]Fire
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的更多相关文章
- Codeforces 864E Fire(DP)
题目链接 Fire 题意 有n个物品,每个物品的挽救时间代价为ti, 消失时刻为di, 价值为pi. 如果要救某个物品,必须在他消失之前救出来. 同一时刻最多只能救一件物品. 当前耗时为当前已经救出的 ...
- Codeforces 864E - Fire(dp)
原题连接:http://codeforces.com/problemset/problem/864/E 题意:一个人想从大火中带走一些东西.每次他只能带一个,耗时ti ,价值为pi, 当总时间超过di ...
- Codeforces 864E Fire(背包DP)
背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...
- H - Fire CodeForces - 864E 01背包
https://codeforces.com/problemset/problem/864/E 这个题目要把这个按照物品毁灭时间进行排序,如果时间短就要排在前面,这个是因为要保证之后的物品的拯救不会影 ...
- Codeforces 864E dp
题意: 房间着火了,里面有n件物品,每件物品有营救需要的时间t,被烧坏的最晚时间d,他的价值p,问能得到的最大价值,并且输出营救出来的物品编号 代码: //必然是先救存活时间短的即d小的,所以先排个序 ...
- codeforce 35C fire again
2017-08-25 17:04:07 writer:pprp 题目描述: • Codeforces 35C Fire Again• N*M的格子,最开始有K个点 (坐标给定) 开始着火• 每一秒着火 ...
- 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 ...
- Codeforces Round #436 (Div. 2) E. Fire
http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...
- Fire Again CodeForces - 35C (BFS)
After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows ...
随机推荐
- JavaScript(第二十六天)【表单处理】
为了分担服务器处理表单的压力,JavaScript提供了一些解决方案,从而大大打破了处处依赖服务器的局面. 一.表单介绍 在HTML中,表单是由<form>元素来表示的,而在JavaS ...
- 用Python满足满足自己的“小虚荣”
首先声明,学习这个只是为了好玩,只是为了好玩,并不是想用这个弄虚作假,做一些不好的事情!一心想做技术人,自制自治! 我们有时候发布一篇日志,或者是一篇博文,总希望自己的浏览量能高点,这样看起来也倍有面 ...
- C语言第二次博客作业---分支结构 陈张鑫
一.PTA实验作业 题目1:计算分段函数[2] 本题目要求计算下列分段函数f(x)的值: 1.实验代码 int main(){double x,y; scanf("%lf",&am ...
- c语言博客作业-指针
一.PTA实验作业 题目1: 1. 本题PTA提交列表 2. 设计思路(用代码表示扣分) 定义整型变量i,count记录平均分,实型变量sum保存总分 for i=0 to n sum = sum+* ...
- 课后练习:C语言实现Linux命令——od
课后练习:C语言实现Linux命令--od --------CONTENTS-------- 题目详情与分析 设计思路 遇到的问题及解决 待实现的设想与思考 学习反思与感悟 附1:myod.c「1.0 ...
- 201621123040《Java程序设计》第十周学习总结
1.本周学习总结 2.书面作业 2.1常用异常 2.1.1自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 算术异常ArithmeticException(除数为0的情况) 类 ...
- 关于python中的operator.itemgetter()函数的用法
1. operator.itemgetter(num)函数 表示对对象的第num维数据进行操作获取. >>>import operator >>>a = [1, 2 ...
- Bate测试报告
1 测试中找出的bug Bug类型 总数 描述 修复的bug 10 1.注册成功并没有直接跳转到登录页面: 2.学校地区无限制,数字也可以: 3.虽然相同用户名不能注册,但是只是显示,注册失败,却没有 ...
- Fluent Interface(流式接口)
我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有Martin fowler大师 所写http://martinfowler.com/bliki/FluentInt ...
- 数据结构与算法 —— 链表linked list(01)
链表(维基百科) 链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储, ...