[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 ...
随机推荐
- Eclipse+Pydev环境搭建
1,准备好Eclipse和JAVA,x64 2,安装JDK,配置JAVA环境变量,假设安装路径为 C:\Program Files\Java\jdk1.8.0_161 在系统变量中,新建CLASSPA ...
- 通过运行一个tomcat容器来记录下初学docker常用的几个命令---容器篇
1.查看容器列表 显示正在运行的容器: [root@localhost HMK]# docker ps 显示所有容器,包括未运行的: [root@localhost HMK]# docker ps - ...
- JAVA接口基础知识总结
1:是用关键字interface定义的. 2:接口中包含的成员,最常见的有全局常量.抽象方法. 注意:接口中的成员都有固定的修饰符. 成员变量:public static final 成员方法 ...
- iOS中滤镜种类及相关介绍
- 【技巧】Java工程中的Debug信息分级输出接口
也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...
- vue jquery js 获取当前时间本周的第一天 和 本月的第一天
交互的时候传输数据 后台要求这样的数据 直接上代码 这是我找度姨要的 附上链接 https://www.cnblogs.com/wasabii/p/7756560.html 它里面有本季度第一天 ...
- js前端对后台数据的获取,如果是汉字则需要添上引号
js前端对后台数据的获取,如果是汉字则需要添上引号
- Python内置函数(60)——compile
英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...
- 以太坊挖矿源码:clique算法
上文我们总结了以太坊最主要的共识算法:ethash算法,本文将重点分析以太坊的另一个共识算法:clique. 关键字:clique,共识算法,puppeth,以太坊地址原理,区块校验,认证结点,POA ...
- 新概念英语(1-143)A walk through the woods
Lesson 143 A walk through the woods 林中散步 Listen to the tape then answer this question. What was so f ...