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 ...
随机推荐
- Spring对注解(Annotation)处理【转】
1.从Spring2.0以后的版本中,spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...
- shell脚本,alias别名命令用法。
[root@localhost ~]# alias alias cp='cp -i' alias mv='mv -i' alias rm='rm -i' [root@localhost ~]# [ro ...
- shell脚本,检查给出的字符串是否为回文
[root@localhost wyb]# .sh #!/bin/bash #检查给出的字符串是否为回文 read -p "Please input a String:" numb ...
- ios之自定义导航栏上的返回按钮
导航栏的按钮,右边的按钮是可以自己随意添加的.但左边的返回按钮怎么定制?你会说,添加一个自己的按钮呗!你可以试试看,这样行不行. 正确的答案是重载UINavigationController类的pus ...
- 74个Swift标准库函数
74个Swift标准库函数 本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swi ...
- emoji等表情符号存mysql的方法
项目中需要存储用户信息(用户昵称有表情符号),自然就遇到了emoji等表情符号如何被mysql DB支持的问题 这里引用先行者博文:https://segmentfault.com/a/1190000 ...
- 更改ubuntu的官方镜像源
我们自己安装的ubuntu通常默认镜像源是官方的,并不好用,因为网速以及限制比较多,所以为了使用方便,通常都会去更改一下默认的镜像源配置. 这里我们使用清华大学开源镜像软件站,https://mirr ...
- C语言实现链表及其操作
#include <stdio.h> #include <stdlib.h> //定义节点 typedef struct Node { int data; struct Nod ...
- Python全栈工程师之html学习笔记
https://www.bilibili.com/video/av15241731 笔记来源:黑马程序员 HTML(Hyper Text Markup Language):超文本标签语言 HTML标签 ...
- shell相关指令介绍$*和$#以及$?和if [[ ! -z $1 ]]
$#,脚本运行时后跟的参数个数 #! /bin/bash case "$#" in 0) printf "Enter a number: " read n=$R ...