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]之间,并且数组全 ...
随机推荐
- 主席树 或者 离散化+分块 BZOJ 4636
4636: 蒟蒻的数列 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 381 Solved: 177[Submit][Status][Discuss ...
- vue安装说明
1.安装node.js(http://www.runoob.com/nodejs/nodejs-install-setup.html) --以下操作在nodejs安装路径下进行(记得不要在C盘)-- ...
- XML-RPC笔记
1.什么是XML-RPC RPC(Remote Procedure Call)就是相当于提供了一种"远程接口"来供外部系统调用,常用于不同平台.不同架构的系统之间互相调用. XML ...
- typeof运算符
javascript中typeof用来判断一个变量或表达式的数据类型. typeof 返回值有六种可能: "number," "string," "b ...
- D - Frog and Portal (利用斐波那契数列的性质)
题目链接:https://cn.vjudge.net/contest/270201#problem/D 具体思路:利用斐波那契数列的性质,斐波那契数列可以构成任何正整数,所以按照顺序减下去肯定能减到0 ...
- Python ctypes中cast/py_object用法
class ctypes.py_object Represents the C PyObject * datatype. Calling this without an argument create ...
- CSS 中 nth-child 和 nth-of-type 的区别
假设有如下代码结构,想要查找 Piggy 那个 p <section> <h1>Words</h1> <p>Little</p> <p ...
- DAY1-GO初识(概述)
一.概述 1.特征 1.1.语法简单:规则严谨.保留指针.但默认阻止指针运算.将切片和字典作为内置类型.更好的维护性: 1.2.并发模型:运行时用GOroutine,一个关键字.简单而自然:搭配cha ...
- 问题解决:The content of the adapter has changed but ListView did not receive a notification
1. 不要在后台线程中直接调用adapter 2. 不要在后台线程中修改adapter绑定的数据 如果对adapter或者adapter绑定的数据是在线程中,加上runOnUiThread就可以了 r ...
- Ubuntu每次开机后提示:检测到系统程序出现问题的解决方法
首先,错误报告存放位置: cd /var/crash/ ls //可以查看错误报告 1 2 sudo rm /var/crash/* //删除该目录下的所有文件 1 但是,这只是删除掉的是错误报告,如 ...