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 ...
随机推荐
- SEO误区之——静态化页面
你随便去找一个做SEO的人或者一个公司,他百分之百会让你把网页弄成纯静态页面,然后告诉你这样对搜索引擎是如何如何地好,那么我告诉你,这个做 SEO的,肯定不专业. 网页静态化这个东西,纯属以讹传讹的事 ...
- Linux下Reids的安装和使用
简单记录一下 redis的官网:https://redis.io/ 官网介绍: Installation Download, extract and compile Redis with: $ wge ...
- Maven项目如何将自定义文件添加到META-INF目录下
Maven项目如何将自定义文件添加到META-INF目录下 学习了:https://blog.csdn.net/yangjiegreat/article/details/78698655 <bu ...
- Java面向对象编程(二)
上一篇博文里总结了面向对象三大特性在Java中的体现.如今谈一谈Java中的抽象类,接口,内部类等特性. 一. 抽象类 public abstract class Shape { public int ...
- C++课程资源下载问题
[来信] 贺老师,您好,我是江西某高校软件学院的一名在校学生.看了您在csdn上公布的博文和视频,我获益良多.不得不承认,之前的大学时光我是荒废了,立即就要大三了,我主攻的是C++方面.因此我悔过自新 ...
- JAVA Eclipse的Android文件结构是怎么样的
默认res目录下面存放了界面需要的布局和图片文件,之所以图片分为hdpi,ldpi,mdpi这些,是为了不同的设备准备的(高/中/低分辨率的图片) Bin目录类似于VS的debug或者releas ...
- GetDlgItem() 出现错误
写MFC程序ASSERT(IsWindow(pTemp->m_hWnd))报错 CRect rect; CWnd *pWnd = GetDlgItem(IDC_picture);//IDC_pi ...
- Node.js学习笔记(3)——关于回调函数和函数的回调
说明:本人是node.js的初学者,尝试向别人解释这是怎么回事是自我学习的一个好方法.如果你发现有些地方并不是那么正确,欢迎提出来让我知道以便修正,共同进步,谢过^_^. 欢迎交流,本人微 ...
- github 迁移google code 项目
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/46692181 转载请一定注明出处. 1,关于google code google ...
- Error executing DDL via JDBC Statement
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 启动hibernate测试案例时报错如下: org.hibernate.tool.schema.spi.CommandAcceptan ...