Codeforces Round #436 (Div. 2) E. Fire(dp 记录路径)
2 seconds
256 megabytes
standard input
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 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.
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.
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.
3
3 7 4
2 6 5
3 7 6
11
2
2 3
2
5 6 1
3 3 5
1
1
1
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.
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 3seconds, but this item will already be completely burned by this time.
【题意】有n件物品,起火了,救每件物品需要花t[i]时间,且必须在d[i]时刻之前完成,救下之后价值为p[i],求最大价值。
【分析】背包吧,dp很好写,dp[i][j]表示在j时刻救下i所得最大价值,关键是记录路径,更新时,将pos[i][j]更新为1,否则更新为0.
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e2+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n,s,ans,now=;
int dp[];
int pos[][];
struct man{
int t,d,p,id;
}a[];
bool cmp(const man&s,const man&t){
if(s.d==t.d)return s.t<t.t;
return s.d<t.d;
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d%d",&a[i].t,&a[i].d,&a[i].p);
a[i].id=i;
}
sort(a+,a++n,cmp);
for(int i=;i<=n;i++){
if(a[i].t>a[i].d)continue;
for(int j=a[i].d-;j>=a[i].t;j--){
if(dp[j]<dp[j-a[i].t]+a[i].p){
dp[j]=dp[j-a[i].t]+a[i].p;
pos[a[i].id][j]=a[i].id;
}
else{
pos[a[i].id][j]=;
}
}
}
int maxn=,now=;
for(int i=;i<=;i++){
if(dp[i]>maxn){
maxn=dp[i];
now=i;
}
}
stack<int>s;
printf("%d\n",maxn);
for(int i=n;i>=;i--){
int id=a[i].id;
if(pos[id][now]){
s.push(id);
now-=a[i].t;
}
}
printf("%d\n",s.size());
while(!s.empty()){
printf("%d ",s.top());
s.pop();
}
return ;
}
Codeforces Round #436 (Div. 2) E. Fire(dp 记录路径)的更多相关文章
- Codeforces Round #436 (Div. 2) E. Fire
http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...
- Codeforces Round #436 (Div. 2) E. Fire(背包+记录路径)
传送门 题意 给出n种物品,抢救第\(i\)种物品花费时间\(t_i\),价值\(p_i\),截止时间\(d_i\) 询问抢救的顺序及物品价值和最大值 分析 按\(d_i\)排序的目的是防止以下情况 ...
- Codeforces Round #436 (Div. 2)【A、B、C、D、E】
Codeforces Round #436 (Div. 2) 敲出一身冷汗...感觉自己宛如智障:( codeforces 864 A. Fair Game[水] 题意:已知n为偶数,有n张卡片,每张 ...
- Codeforces Round #131 (Div. 1) B. Numbers dp
题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...
- Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...
- Codeforces Round #260 (Div. 1) A - Boredom DP
A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...
- Codeforces Round #436 (Div. 2)
http://codeforces.com/contest/864 第一次打cf的月赛-- A 题意:给你一个数列,问你能不能保证里面只有两种数且个数相等.2<=n<=100,1<= ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
随机推荐
- Fire Net(深度优先搜索)
ZOJ Problem Set - 1002 Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we ha ...
- asp.net中模拟测试smtp发邮件
最近在编程人生里要测试一个会员邮件的功能,就写了下面的代码. 在asp.net 中,有时要测试发信SMTP,但如果在单元测试中,如果没方便好用的 smtp怎么办,其实还是有办法模拟的,下面讲解下: 在 ...
- Verilog笔记.4.inout端口
inout是一个双向端口,实现为使用三态门,第三态为高阻态‘z’. 在实际电路中高阻态意味着响应的管脚悬空.断开. 当三态门的控制信号为真时,三态门选通,作输出端口使用:控制信号为假时,三态门是高阻态 ...
- i春秋第二届春秋欢乐赛RSA256writeup
i春秋第二届春秋欢乐赛writeup 下载之后进行解压 发现四个文件 0x01看到题目是RSA的 又看到public.key 所以直接用kali linux的openssl 0x02可以看到e就是E ...
- 一个TCP报文段的数据部分最多为多少个字节,为什么
IP数据报的最大长度=2^16-1=65535(字节)TCP报文段的数据部分=IP数据报的最大长度-IP数据报的首部-TCP报文段的首部=65535-20-20=65495(字节) 一个tcp报文段的 ...
- 教你如何修改FireFox打开新标签页(NewTab Page)的行列数
FireFox的打开新建标签页(即NewTab Page)默认只能显示3x3个网站缩略图,这9个自定义的网站,非常方便快捷,什么hao123的弱爆了,本人从未用过此类导航网站,曾经用过的也只是abou ...
- OC学习篇之---类的延展
来源:http://blog.csdn.net/jiangwei0910410003/article/details/41775603 前一篇文章我们介绍了类的类目概念和使用:http://blog. ...
- Netty框架入门
一.概述 Netty是由JBOSS提供的一个java开源框架. Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 二. ...
- Oracle安装出现报错
报错信息如下: >>> Couldnot execute auto check for display colors using command /usr/bin/xdpyinfo. ...
- [ python ] 软件开发规范
在python开发中,我们建议采用如下规范: soft/ ├── bin # 程序执行文件目录 │ ├── __init__.py │ └── start.py # 程序开始执行脚本文件 ├─ ...