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\)排序的目的是防止以下情况 ...
随机推荐
- 后缀数组SA
复杂度:O(nlogn) 注:从0到n-1 const int maxn=1e5; char s[maxn]; int sa[maxn],Rank[maxn],height[maxn],rmq[max ...
- Java初学者最近三次作业的心得体会
作为一个初学者,简单的谈一下自己的作业心得体会.如果你是完全没有接触过Java的学习,本篇博文可能会有些收获,如果你已经学习Java有一段时间了,那么可以放弃这篇文章了,因为这篇文章讲解的是基本的东西 ...
- Spring Boot 静态文件,请求不到,util文件夹
静态文件貌似对util文件夹有特殊处理static/js/test.js 可以请求到static/js/laydate/test.js 可以请求到static/js/util/test.js 请求不到
- 使用react脚手架create-react-app创建react应用
Create React App是一种官方支持的创建单页React应用程序的方法.它提供了一个没有配置的现代构建设置. 一.全局安装脚手架: npm install -g create-react-a ...
- TensorFlow框架 入门笔记
背景 略 基础 介绍 略 TensorFlow安装 link TensorFlow 主要概念 使用图(graph)来表示计算任务(执行流程). 在被称之为会话(session)的上下文(context ...
- 码云git本地仓库链接远程仓库
原文链接: 点我 git提交时,仓库是空的,本地有源码 应该打开cmd 归到项目路径 然后输入git push -u origin master -f 是把本地的项目强制推送到空的仓库 git ...
- docker部署gitlab
Docker部署gitlab 一.前提条件 (1) 存在docker (2) 服务器可以联网(外网) (3) 服务器内存至少4G(内存不够会出现502错误) 内存不足502错误 ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- Spring 中基于 AOP 的 @AspectJ注解实例
@AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格.通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的 ...
- 【Scala】什么是隐式转换?它又能用来干嘛?该怎么用
文章目录 定义 隐式参数 隐式转换 隐式值:给方法提供参数 隐式视图 将Int和Double类型转换为String 狗狗学技能(使用别的类中的方法) 使用规则 定义 隐式参数 隐式参数指在函数或者方法 ...