Codeforces 864E Fire(DP)
题目链接 Fire
题意 有n个物品,每个物品的挽救时间代价为ti, 消失时刻为di, 价值为pi。
如果要救某个物品,必须在他消失之前救出来。
同一时刻最多只能救一件物品。
当前耗时为当前已经救出的物品的ti累积。
你需要救出总价值尽可能大的物品,并输出方案。
考虑DP
f[i][j]为考虑前i个物品,获得总价值为j的时候,所用时间的最小值。
c[i][j]为在搜索到第i件物品,当前总价值为j的时候下一步的价值搜索状态。
则有f[i][j] = f[i - 1][j - p[i]] + t[i]
取最大值的时候考虑最大的i满足f[n][i] != INF即可。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 105;
const int Q = 2010; struct node{
int t, d, p;
int id;
friend bool operator < (const node &a, const node &b){
return a.d < b.d;
}
} a[N]; int f[N][Q], c[N][Q];
int n, cnt = 0;
int ans[N]; void solve(int i, int j){
if (i == 0) return;
if (c[i][j] != j) ans[++cnt] = a[i].id;
solve(i - 1, c[i][j]);
} int main(){ scanf("%d", &n);
rep(i, 1, n){
scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p);
a[i].id = i;
} sort(a + 1, a + n + 1); rep(i, 0, n) rep(j, 0, 2001) f[i][j] = 1 << 30;
f[0][0] = 0;
rep(i, 1, n){
rep(j, 0, 2000){
f[i][j] = f[i - 1][j];
c[i][j] = j;
if (j < a[i].p) continue;
int now = f[i - 1][j - a[i].p] + a[i].t;
if (now < f[i][j] && now < a[i].d){
f[i][j] = now;
c[i][j] = j - a[i].p;
}
}
} dec(i, 2000, 0) if (f[n][i] < (1 << 30)){
printf("%d\n", i);
solve(n, i);
printf("%d\n", cnt);
dec(j, cnt, 1) printf("%d ", ans[j]);
putchar(10);
break;
} return 0;
}
Codeforces 864E Fire(DP)的更多相关文章
- Codeforces 864E Fire(背包DP)
背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Codeforces 1110D Jongmah (DP)
题意:你有n个数字,范围[1, m],你可以选择其中的三个数字构成一个三元组,但是这三个数字必须是连续的或者相同的,每个数字只能用一次,问这n个数字最多构成多少个三元组? 解析:首先我们容易发现,我们 ...
- Codeforces 837D--Round Subset (DP)
原题链接:http://codeforces.com/contest/837/problem/D 题意:在n个数字中,取k个数,使这些数的乘积后缀“0”的个数最大,输出后缀0的最大数量. 思路:显然只 ...
- CodeForces 455A Boredom (DP)
Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
随机推荐
- Feign-请求不同注册中心的服务
场景 需要通过Feign Client请求,其他注册中心或者其他Restful服务. 临时方案 Feign 请求转为RestTemplate http请求. 优点:能适应,feign环境和非feign ...
- tomcat假死现象 - 二
1 编写背景 最近服务器发现tomcat的应用会偶尔出现无法访问的情况.经过一段时间的观察最近又发现有台tomcat的应用出现了无法访问情况.简单描述下该台tomcat当时具体的表现:客户端请求没有响 ...
- Python IDE推荐
八个最佳Python IDE 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Python是一种功能强大.语言简洁的编程语言.本文向大家推荐8个适合pyt ...
- 【简●解】 LG P2730 【魔板 Magic Squares】
LG P2730 [魔板 Magic Squares] [题目背景] 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 ...
- 基础训练 Huffuman树
Huffuman树 /*解法一*/ #include<iostream> #include<queue> using namespace std; int main(){ pr ...
- docker+Battery Historian 环境搭建(电量分析)
docker 安装(windows) 1. 下载 https://docs.docker.com/docker-for-windows/install/ 和 安装和添加环境变量(...) 2. 安 ...
- 【03】图解原型和原型链by魔芋
[03]图解原型和原型链 一图胜前言 请先结合图解原型和原型链这张图. 可以分为4种情况. 情况1: Object有: constructor:是Function. __pro ...
- Head First HTML5 Programming笔记--chapter2 介绍Javascript和DOM
你已经了解了HTML标记(也称为结构),而且知道了CSS样式(也称为表示),剩下的就是Javascript(也称为行为). JavaScript的工作方式 1. 编写 你创建HTML标记和JavaSc ...
- Consecutive Subsequence (DP+map)
You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...
- 贴一下我写过的c++程序代码
5258 #include <iostream>#include <iomanip>#include <cmath>using namespace std;clas ...