CodeForces - 922E Birds —— DP
题目链接:https://vjudge.net/problem/CodeForces-922E
1 second
256 megabytes
standard input
standard output
Apart from plush toys, Imp is a huge fan of little yellow birds!
To summon birds, Imp needs strong magic. There are n trees in a row on an alley in a park, there is a nest on each of the trees. In the i-th nest there are ci birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him costi points of mana. However, for each bird summoned, Imp increases his mana capacity by B points. Imp summons birds one by one, he can summon any number from 0 to ci birds from the i-th nest.
Initially Imp stands under the first tree and has W points of mana, and his mana capacity equals W as well. He can only go forward, and each time he moves from a tree to the next one, he restores X points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?
The first line contains four integers n, W, B, X (1 ≤ n ≤ 103, 0 ≤ W, B, X ≤ 109) — the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.
The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ 104) — where ci is the number of birds living in the i-th nest. It is guaranteed that .
The third line contains n integers cost1, cost2, ..., costn (0 ≤ costi ≤ 109), where costi is the mana cost to summon a bird from the i-th nest.
Print a single integer — the maximum number of birds Imp can summon.
2 12 0 4
3 4
4 2
6
4 1000 10 35
1 2 4 5
1000 500 250 200
5
2 10 7 11
2 10
6 1
11
In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6.
In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.
题意:
有n棵树,在第i棵树上有ci只小鸟,在此树召唤一只小鸟需要花费costi个mama,但却能增加b个单位装mama的容量。人初始时在第一棵树下,且只能往前走,但每往前走一棵树,就能得到x个mama,前提是不能超过容量,初始容量和mama值都为w。问在第n棵树时,最多能召唤多少只小鸟。
题解:
1.一看题目,就很容易想到DP递推:设 dp[i][cnt]为到达第i棵树,且剩余cnt个mama的情况下,最多能召唤多少只小鸟。
2.但是,在看看数据:w、cost等有关mama的大小范围都为:1e9,数组根本开不了这么大。而却规定了:,多么(找不到形容词)。
3.根据数据范围,重新调整一下dp数组,设dp[i][j]为:到达第i棵树,且召唤了j只小鸟所剩下的mama的最大值。
4.那么就可以根据此递推。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; LL dp[][]; //直接开1e3*1e4,内存开销太大,故使用滚动数组
int c[], cost[];
int main()
{
int n, w, b, x;
while(scanf("%d%d%d%d", &n,&w,&b,&x)!=EOF)
{
int sum = ;
for(int i = ; i<=n; i++) scanf("%d", &c[i]), sum += c[i];
for(int i = ; i<=n; i++) scanf("%d", &cost[i]); memset(dp, -, sizeof(dp));
for(int j = ; j<=c[]; j++) //初始化第一棵树
dp[][j] = 1LL*w-1LL*j*cost[];
for(int i = ; i<=n; i++)
{
memset(dp[i&], -, sizeof(dp[i&])); //记得初始化
for(int j = ; j<=sum; j++)
{
for(int k = ; k<=min(j,c[i]); k++)
{
//小于0,即不可能达到的状态,必须退出。否则往后可能又会变成正,认为此状态是存在的,对答案有影响。
if(dp[(i-)&][j-k]<) continue;
LL tmp = min(1LL*w+1LL*(j-k)*b, dp[(i-)&][j-k]+x)-1LL*k*cost[i];
dp[i&][j] = (dp[i&][j]==-)?tmp:max(dp[i&][j],tmp);
}
}
} int cnt = sum;
while(cnt && dp[n&][cnt]<) cnt--;
printf("%d\n", cnt);
}
}
CodeForces - 922E Birds —— DP的更多相关文章
- [Codeforces 922E]Birds
Description 题库链接 一条直线上有 \(n\) 棵树,每棵树上有 \(c_i\) 只鸟,在一棵树底下召唤一只鸟的魔法代价是 \(cost_i\) 每召唤一只鸟,魔法上限会增加 \(B\) ...
- 2018.12.14 codeforces 922E. Birds(分组背包)
传送门 蒟蒻净做些水题还请大佬见谅 没错这又是个一眼的分组背包. 题意简述:有n棵树,每只树上有aia_iai只鸟,第iii棵树买一只鸟要花cic_ici的钱,每买一只鸟可以奖励bbb块钱,从一棵 ...
- codeforces 682D(DP)
题目链接:http://codeforces.com/contest/682/problem/D 思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列 ...
- codeforces 666A (DP)
题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...
- Codeforces 264B 数论+DP
题目链接:http://codeforces.com/problemset/problem/264/B 代码: #include<cstdio> #include<iostream& ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- CodeForces 512B(区间dp)
D - Fox And Jumping Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- Linux学习之十六-Linux用户管理
Linux用户管理 Linux系统跟Windows系统一样,可以创建不同的用户,不同的用户组.在不同用户下使用系统具有相应的权限 创建一个普通用户时,会修改几个文件,拷贝一些初始文件到用户家目录中 修 ...
- 【重点突破】—— Vue1.0到Vue2.0的变化
前言: 本文参考作者:_So_ 和 我是某慧 的博文,重点梳理Vue1.0升级到Vue2.0后在开发中要注意的不同,以做学习. 组件模板不再支持片段代码,必须有一个顶级元素包裹,例如: ...
- 转: 学习Javascript闭包(Closure) (阮一峰)
from: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html
- sql 从另外一张表查询数据存入本表. (有关联的)
UPDATE friends INNER JOIN users ON friends.friendid=users.userid SET friends.friendname=users.userna ...
- vue2 axios 接口函数封装
封装 axios 工具,编辑 src/api/index.js 文件 首先,我们要使用 axios 工具,就必须先安装 axios 工具.执行下面的命令进行安装 npm install axios - ...
- Win8 恢复传统启动菜单 for 多系统
如果你安装了Win7和Win8,启动的时候得先启动到Win8然后再启动到Win7,很怀念原来的启动选择器,这里给你方法了~ 当前系统是Win8的输入 bcdedit /set {current} bo ...
- (一)关于jQuery的网上资源
jQuery官网: http://jquery.com/ jQuery API: http://jquery.cuishifeng.cn/ w3school学习网站:http://www.w3scho ...
- Spring里通过注解开启事物
方式1 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://w ...
- JVM中垃圾收集选项
最初并发垃圾收集被引入的时候,激活并发垃圾收集的命令选项是: -XX:+UseParallelGC 增强的并行收集和Java 6一起发布,通过一个新的命令行选项: -XX:+UseParallelOl ...
- java8笔记: sorted()之正序倒序
java8笔记: sorted()之正序倒序 这篇文章将会讲解Java 8 Stream sorted()示例 下面代码以自然序排序一个list List<Person> listTem ...