E. Fire
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 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.

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.

Examples
Input
3
3 7 4
2 6 5
3 7 6
Output
11
2
2 3
Input
2
5 6 1
3 3 5
Output
1
1
1
Note

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.

典型背包题

但是要排序先,因为如果有一个任务的deadline是8,时间是5,,然后价值是100861,另一个任务的deadline是2,时间是1,价值是1,那么第二个任务不会被计入,就产生了错误

就是不具有那种一般背包的元素随便放的条件这个是有一个限制条件在deadline前,那么从小到大用deadline排序就可以变成基本的一维背包了

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
struct node
{
int ed,ne,val,id;
bool operator < (const node &A)const{
return ed<A.ed;
}
} e[N];
struct ct
{
int val;
vector<int>s;
ct()
{
val=;
s.clear();
}
} dp[N<<];
int main()
{
int maxx=,n;
scanf("%d",&n);
for(int i=; i<=n; ++i)
{
scanf("%d%d%d",&e[i].ne,&e[i].ed,&e[i].val);
maxx=max(maxx,e[i].ed);
e[i].id=i;
}
sort(e+,e+n+);
for(int i=; i<=n; ++i)
{
if(e[i].ed<=e[i].ne) continue;
for(int j=e[i].ed; j>=; --j)
{
if(j-e[i].ne<=) break;
if(dp[j].val<dp[j-e[i].ne].val+e[i].val)
{
dp[j].val=dp[j-e[i].ne].val+e[i].val;
dp[j].s.clear();
for(int k=; k<(int)dp[j-e[i].ne].s.size(); ++k) dp[j].s.push_back(dp[j-e[i].ne].s[k]);
dp[j].s.push_back(e[i].id);
}
}
}
int k=maxx;
for(int i=;i<=maxx;++i) if(dp[i].val>dp[k].val) k=i;
printf("%d\n%d\n",dp[k].val,dp[k].s.size());
for(int i=; i<(int)dp[k].s.size(); ++i) printf("%d ",dp[k].s[i]);
puts("");
}

这个是别人的干净清爽的代码。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = ;
const int M = ;
int n;
int dp[M], vis[N][M];
struct node {
int t, d, p, id;
bool operator < (const node&r) const{
return d < r.d;
}
}a[N];
int b[N];
int main() {
int i, j, k, t, ed = , cnt = ;
memset(dp, , sizeof(dp)); memset(vis, , sizeof(vis));
scanf("%d", &n);
for(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);
for(i = ; i <= n; ++i) {
t = a[i].t;
for(j = a[i].d-; j >= t; --j) {
if(dp[j] < dp[j-t]+a[i].p) {
dp[j] = dp[j-t] + a[i].p;
vis[i][j] = ;
}
}
}
for(i = ; i < a[n].d; ++i) if(dp[i]>dp[ed]) ed = i;
printf("%d\n", dp[ed]);
for(i = n; i >= ; --i) {
if(vis[i][ed]) {b[cnt++] = a[i].id; ed -= a[i].t;}
}
printf("%d\n", cnt);
for(i = cnt-; i > ; --i) printf("%d ", b[i]);
if(cnt) printf("%d\n", b[]);
return ;
}

codeforce E. Fire背包的更多相关文章

  1. Codeforce E. Fire

    E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  2. codeforce 35C fire again

    2017-08-25 17:04:07 writer:pprp 题目描述: • Codeforces 35C Fire Again• N*M的格子,最开始有K个点 (坐标给定) 开始着火• 每一秒着火 ...

  3. codeforces 864 E. Fire(背包+思维)

    题目链接:http://codeforces.com/contest/864/problem/E 题解:这题一看就很像背包但是这有3维限制也就是说背包取得先后也会对结果有影响.所以可以考虑sort来降 ...

  4. UVALive 5066 Fire Drill BFS+背包

    H - Fire Drill Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  5. Coderfroces 864 E. Fire(01背包+路径标记)

    E. Fire http://codeforces.com/problemset/problem/864/E Polycarp is in really serious trouble — his h ...

  6. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  7. Codeforces Round #436 E. Fire(背包dp+输出路径)

    题意:失火了,有n个物品,每个物品有价值pi,必须在时间di前(小于di)被救,否则就要被烧毁.救某个物 品需要时间ti,问最多救回多少价值的物品,并输出救物品的顺序. Examples Input ...

  8. Codeforces 864E Fire(背包DP)

    背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...

  9. Codeforces Round #436 (Div. 2) E. Fire(背包+记录路径)

    传送门 题意 给出n种物品,抢救第\(i\)种物品花费时间\(t_i\),价值\(p_i\),截止时间\(d_i\) 询问抢救的顺序及物品价值和最大值 分析 按\(d_i\)排序的目的是防止以下情况 ...

随机推荐

  1. docker-compose简介及安装

    一.简介 Compose是用于定义和运行多容器Docker应用程序的工具,是docker的服务编排工具,主要应用于构建基于Docker的复杂应用,compose通过一个配置文件来管理多个docker容 ...

  2. 学习 .net core 3----蒋金楠 笔记 构建 Asp.net core Web应用

    前言:准备系统的学习一下.net core 所以购买了 蒋金楠的 ASP.NET CORE 3 书籍,为了加深印象 特此笔记,会持续更新到学习完为止 使用  命令行   dotnet  new  co ...

  3. 47000名开发者每月产生30000个漏洞 微软是如何用AI排查的

    目前微软共有 47000 多名开发人员,每月会产生将近 30000 个漏洞,而这些漏洞会存储在 100 多个 AzureDevOps 和 GitHub 仓库中,以便于在被黑客利用之前快速发现关键的漏洞 ...

  4. 控制台报错 [WDS] Disconnected!

    Webpack 的 HMR 功能,是通过 WebSocket 实现的推送 JSON Patch,同时需要第三方库支持. 具体解决方案: 热加载(HMR)是 Webpack Dev Server 最强大 ...

  5. 将不确定变成确定~LINQ DBML模型可以对应多个数据库吗

    答案是肯定的,一个DBML模型可以对应多个数据库,只要数据库中的表与模型中定义的表结构完成相同,就可以这个技术,我们可以用来开发一些通用的功能模块,如通过后台管理模块,我们将一些通用表进行抽象,如,对 ...

  6. Spring Cloud 学习 之 Spring Cloud Eureka(搭建)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...

  7. jvm入门及理解(五)——运行时数据区(虚拟机栈)和本地方法接口

    一.虚拟机栈背景 由于跨平台性的设计,java的指令都是根据栈来设计的.不同平台CPU架构不同,所以不能设计为基于寄存器的. 优点是跨平台,指令集小,编译器容易实现,缺点是性能下降,实现同样的功能需要 ...

  8. 日志系列1——slf4j日志框架原理

    目录 1.前言 2.日志门面 3.日志库 4.日志适配器 5.日志库的选用 6.logback.xml 配置文件 1.前言 ​ 说到日志工具,日常工作或学习中肯定听过这些名词:log4j.logbac ...

  9. IOS App打包发布完整流程

    注册成为开发者 登录苹果开发者中心,点击Accounts,在这里需要你填写你的Appple ID进行登录,如果没有,点击这里申请一个,填写信息就成,这里就不再赘述.申请完成之后,使用申请的AppID进 ...

  10. 解决:idea中右键项目找不到subversion

    2019.02版IDEA,刚刚发现更新不了项目,但是我记得之前的项目是可以直接更新的.然后,我打开之前的项目找到相关项,对比了一下,找到了方法: file--settings--Version Con ...