Coderfroces 864 E. Fire(01背包+路径标记)
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 ti seconds 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 2 seconds. 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 3 seconds, but this item will already be completely burned by this time.
01背包多了一个控制条件,还有题目要求打印任意一种,但必须按照顺序(不是字典序),因此需要对d排序了。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/sTACK:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
struct point
{
int t;
int d;
int p;
int id;
friend bool operator<(const point &a,const point &b)
{
return a.d<b.d;
}
}e[];
int n,x,y,z,k=;
int dp[];
int path[][];
int vis[];
int main()
{
scanf("%d",&n);
int pos=;
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&x,&y,&z);
if(x>=y) continue;
e[++k].t=x;
e[k].d=y;
e[k].p=z;
e[k].id=i;
pos=max(pos,y);
}
sort(e+,e+k+);
memset(dp,,sizeof(dp));
memset(path,,sizeof(path));
for(int i=;i<=k;i++)
{
for(int j=e[i].d-;j>=e[i].t;j--)
{
if(dp[j]<(dp[j-e[i].t]+e[i].p))//中间变量注意边界。
{
dp[j]=dp[j-e[i].t]+e[i].p;
path[i][j]=;
}
}
}
int ans=-,cnt=;
for(int i=;i<=pos;i++)
{
ans=max(ans,dp[i]);
}
for(int j=pos;j>=;j--)
{
if(dp[j]==ans)
{
for(int i=k,s=j;i>= && s>=;i--)
{
if(path[i][s])
{
vis[cnt++]=e[i].id;
s-=e[i].t;
}
}
break;
}
}
reverse(vis,vis+cnt);
printf("%d\n%d\n",ans,cnt);
for(int i=;i<cnt;i++)
{
if(i) printf(" ");
printf("%d",vis[i]);
}
return ;
}
Coderfroces 864 E. Fire(01背包+路径标记)的更多相关文章
- UVA 624 ---CD 01背包路径输出
DescriptionCD You have a long drive by car ahead. You have a tape recorder, but unfortunately your b ...
- UVA 624 CD【01背包+路径记录】
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
- UVA--624 CD(01背包+路径输出)
题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Happy Programming Contest(ZOJ3703)(01背包+路径储存)
Happy Programming Contest ZOJ3703 老实说:题目意思没看懂...(希望路过的大神指点) 最后那个the total penalty time是什么意思啊!!! 还是学 ...
- uva624 CD (01背包+路径的输出)
CD Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 624 ...
- HDU 6083 度度熊的午饭时光(01背包+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=6083 题意: 思路: 01背包+路径记录. 题目有点坑,我一开始逆序枚举菜品,然后一直WA,可能这样的话路径记录 ...
- UVA624 CD,01背包+打印路径,好题!
624 - CD 题意:一段n分钟的路程,磁带里有m首歌,每首歌有一个时间,求最多能听多少分钟的歌,并求出是拿几首歌. 思路:如果是求时常,直接用01背包即可,但设计到打印路径这里就用一个二维数组标记 ...
- 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径
链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...
- 01背包记录路径 (例题 L3-001 凑零钱 (30分))
题意: 就是找出来一个字典序最小的硬币集合,且这个硬币集合里面所有硬币的值的和等于题目中的M 题解: 01背包加一下记录路径,如果1硬币不止一个,那我们也不采用多重背包的方式,把每一个1硬币当成一个独 ...
随机推荐
- OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法
本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法. 这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...
- jQuery -> 怎样【先创建、再改动、后加入】 DOM元素
怎样一气呵成地.on the fly地操作DOM元素呢? 比如顺序运行[创建]-> [改动]-> [加入]三个动作. 因为jQuery支持链式操作,事实上就是设计模式的builder模式, ...
- UVALive 6084 Happy Camper(数学题)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- wireshark界面调整成英文的
https://ask.wireshark.org/questions/48823/change-the-gui-language 英文版设置 From the Edit (Bearbeiten) m ...
- powerdesigner里的table背景色是不是可以修改的?
Tools->Display Preferences->Format->Table->Modify->Fill->Fill color:
- 鼠标滑过,解决ul下 li下a的背景与父级Li不同宽的问题
我们在写导航或者页面有超链接的地方,有一些是需要超链接的背景和Li的宽度一样的.但是,却没有达到这种效果?为什么? 我们做的效果图:如下 期望的效果:如下 出现这样的原因:由于a是个行内元素,它没有宽 ...
- dedecms后台登录,与后台界面去除多于的样式
http://jingyan.baidu.com/article/597035520f4edc8fc00740f7.html
- php时间戳转化成时间相差8小时问题
php时间戳 转化成时间的时候 $mytime=time(); echo $mytime.'<br />'; echo date('Y-m-d H:i:s',$mytime); 会产生8个 ...
- SSD-tensorflow-2 制作自己的数据集
VOC2007数据集格式: VOC2007详细介绍在这里,提供给大家有兴趣作了解.而制作自己的数据集只需用到前三个文件夹,所以请事先建好这三个文件夹放入同一文件夹内,同时ImageSets文件夹内包含 ...
- ubutun lunix 64安装neo4j 图形数据库
借鉴链接: https://my.oschina.net/zlb1992/blog/915038