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 ...
随机推荐
- Zabbix的前台SQL注射漏洞利用
今年8月份Map在wooyun上发了个Zabbix某前台SQL注射漏洞 ,11月份才公开. 漏洞详情大约是这样的: 在zabbix前端存在一个SQL注射漏洞,由于zabbix前台可以在zabbix的s ...
- koajs 项目实战(一)
(一)koa 1.Koa(koajs)-- 基于 Node.js 平台的下一代 web 开发框架 koa1 npm install koa -g npm install koa-generator ...
- 【DB2】DB2使用IMPORT命令导入含有自增长列的表报错处理
1.启动数据库:db2start 2.创建数据库:create db TestDB using codeset gbk territory CN collate using identity 3.连 ...
- Node.js学习笔记(5)——关于child_process模块
child_process是node一个比较重要的模块,通过它可以实现创建多线程,来利用多核CPU. 这个模块提供了四个创建子进程的函数. spawn.exec.execFile.fork. spaw ...
- sql数据库log自动增长被取消
原因分析:数据库可分配空间为0 解决方法:增加数据库初始大小
- iostat命令具体解释——linux性能分析
之前总结uptime和free命令,今天继续来总结一下iostat.给自己留个笔记.同一时候也希望对大家实用. 版本号信息: sysstat version 9.0.4 (C) S ...
- Django之便签生成
myblog_tag.py #coding:utf-8 __author__ = 'similarface'from django import template register=template. ...
- c# .net Global.asax文件的作用
1 Global.asax文件的作用 先看看MSDN的解释,Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选的文件,该文件包含响应 ASP.NET 或HTTP模块所引发的 ...
- CentOS下安装python3.x版本
现在python都到了3.x版本,但是centos中自带的python仍然是2.7版本的,所以想把python换成3.x版本的. 但是这个地方有个坑,你要是直接编译安装了python3.x之后,估计你 ...
- AI生万物,新世界的大门已敞开
四月是万物复苏的时节,一年一度的GMIC全球移动互联网大会也在这个时间如期而至,在4月26日-28日的会议期间,有超过三百位行业专家进行了精彩的演讲,更有数万名现场观众感受到思维碰撞迸发出的火花. 作 ...