codeforce E. Fire背包
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 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.
典型背包题
但是要排序先,因为如果有一个任务的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背包的更多相关文章
- Codeforce E. Fire
E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- codeforce 35C fire again
2017-08-25 17:04:07 writer:pprp 题目描述: • Codeforces 35C Fire Again• N*M的格子,最开始有K个点 (坐标给定) 开始着火• 每一秒着火 ...
- codeforces 864 E. Fire(背包+思维)
题目链接:http://codeforces.com/contest/864/problem/E 题解:这题一看就很像背包但是这有3维限制也就是说背包取得先后也会对结果有影响.所以可以考虑sort来降 ...
- UVALive 5066 Fire Drill BFS+背包
H - Fire Drill Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
- Coderfroces 864 E. Fire(01背包+路径标记)
E. Fire http://codeforces.com/problemset/problem/864/E Polycarp is in really serious trouble — his h ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- Codeforces Round #436 E. Fire(背包dp+输出路径)
题意:失火了,有n个物品,每个物品有价值pi,必须在时间di前(小于di)被救,否则就要被烧毁.救某个物 品需要时间ti,问最多救回多少价值的物品,并输出救物品的顺序. Examples Input ...
- Codeforces 864E Fire(背包DP)
背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...
- Codeforces Round #436 (Div. 2) E. Fire(背包+记录路径)
传送门 题意 给出n种物品,抢救第\(i\)种物品花费时间\(t_i\),价值\(p_i\),截止时间\(d_i\) 询问抢救的顺序及物品价值和最大值 分析 按\(d_i\)排序的目的是防止以下情况 ...
随机推荐
- SAP WM TO Print Control设置里,Movement Type 的优先级更高
SAP WM TO Print Control设置里,Movement Type 的优先级更高 存储类型的配置: 从storage type GRM 搬到任何地方,都不需要打印TO单. 移动类型的配置 ...
- Vue.js中scoped引发的CSS作用域探讨
前言 在Vue.js的组件化开发中,常常会对某个组件的style标签加上scoped属性,如<style lang='less' scoped>,这样做的目的在于使这个组件的样式不能轻易在 ...
- HTML5学习笔记之表格标签
HTML5学习笔记之表格标签 其他HTML5相关文章 HTML5学习笔记之HTML5基本介绍 HTML5学习笔记之基础标签 HTML5学习笔记之表格标签 HTML5学习笔记之表单标签 HTML5学习笔 ...
- 自动安装带nginx_upstream_check_module模块的Nginx脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/bin/bash useradd -s /sbin/no ...
- CF思维联系– Codeforces-989C C. A Mist of Florescence
ACM思维题训练集合 C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes ...
- Nmon 的安装及使用
一.安装 Nmon 1.下载地址:http://nmon.sourceforge.net/pmwiki.php?n=Site.Download 2.下载对应系统的 nomn 工具(我用 centos6 ...
- B. Working out 四角dp
https://codeforces.com/problemset/problem/429/B 这个题目之前写过,不过好像..忘记了,今天又没有写出来,应该之前没有想明白... 这个应该算一个四角dp ...
- Scrapy+selenium爬取简书全站
Scrapy+selenium爬取简书全站 环境 Ubuntu 18.04 Python 3.8 Scrapy 2.1 爬取内容 文字标题 作者 作者头像 发布日期 内容 文章连接 文章ID 思路 分 ...
- IDEA中如何使用debug调试项目 一步一步详细教程
转载该文章:https://blog.csdn.net/yxl_1207/article/details/80973622 一.Debug开篇 首先看下IDEA中Debug模式下的界面. 如下是在ID ...
- 【HBase】集群搭建/安装部署
目录 第一步:下载对应的HBase安装包 第二步:上传压缩包并解压 第三步:修改配置文件 第四步:安装包分发到另外两台机器 第五步:三台机器创建软连接 第六步:三台机器添加环境变量 第七步:启动HBa ...