网页链接:点击打开链接

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?

Input

The first line contains four integers nWBX (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.

Output

Print a single integer — the maximum number of birds Imp can summon.

Examples
input

Copy
2 12 0 4
3 4
4 2
output
6
input

Copy
4 1000 10 35
1 2 4 5
1000 500 250 200
output
5
input

Copy
2 10 7 11
2 10
6 1
output
11
Note

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棵树,刚开始有w元,第i棵树上有nb[i]只鸟,第i棵树上的鸟要花c[i]元,每走一棵树增加x元,每买一个鸟会让钱包容量增加b,问最多能买到几只鸟?

解法:背包dp,不过这题要根据数据范围选好下标,下标不能是1e9的钱数,dp的值不能是鸟数,鸟数可以用来当成下标

dp[i][j]表示走到第i棵树下,这时候已经买了j只鸟,剩下的钱数,dp[i][j] = max{dp[i-1][j - k] - k * c[i-1] + x}

坑点:每次更新dp[i][j]的时候钱不能超过钱包容量,而且要算出买几只鸟能让dp[i][j]最大,所以用了个嵌套的max,min,但是!!我居然学别人在开头define了max和min,导致嵌套了个寂寞,以后要么自己定义函数,要么直接用algorithm中的max,别再define了!

代码里还是有蛮多细节技巧的,仔细看看

#include<cstdio>
#include<cstring>
#include<algorithm>
//#define min(a, b) a>=b?b:a //←罪魁祸首!!!
//#define max(a, b) a>=b?a:b //←你也是!!!!
typedef long long ll;
using namespace std; const int maxn = 1000 + 100;
const int maxw = 10000 + 100;
ll c[maxn], nb[maxw];
ll dp[maxn][maxw];//dp[i][j]表示走到第i棵树下,这时候已经买了j只鸟,剩下的钱数
//dp[i][j] = max{dp[i-1][j - k] - k * c[i-1] + x} int main(){
ll n, w, b, x;//走一棵树加x钱,买一只鸟增加容量b;
scanf("%lld %lld %lld %lld", &n, &w, &b, &x);
int mana = w, max_mana = w;
for(int i = 0; i < n; i++) scanf("%d", &nb[i]);
for(int i = 0; i < n; i++) scanf("%d", &c[i]);
ll sum = 0;
memset(dp, -1, sizeof(dp));//最后还为-1的dp就是不可能达到的
dp[0][0] = w;//当i等于0的时候,即在第一棵树下的时候,这时候一只鸟都没买,所以当i=0时只有j=0这种情况
for(int i = 1; i <= n; i++){//从刚到第二棵树下开始循环(正在第二颗树下,还没决定在第二颗树买几只鸟)
sum += nb[i-1];//此时站在第i棵树下,最多买了sum只鸟,也就是前面的全买了 for(int j = 0; j <= sum; j++){
for(int k = 0; k <= nb[i-1] && k <= j ; k++){//这个循环用来解决dp[i][j]的最大值能是多少
if(dp[i-1][j-k] == -1){/*printf("j = %d, k = %d, dp[%d][%d] = %d\n", j, k, i-1, j-k,dp[i-1][j-k]);*/continue;}
if(dp[i-1][j-k] - k * c[i-1] < 0 ) {/*printf("nonono!\nk = %d, j = %d\n", k, j);*/continue;}//没钱了, 这时候还没走到下一棵树,所以不要加x
dp[i][j] = max(dp[i][j], min(dp[i-1][j-k] - k*c[i-1] + x, w + j * b));//更新最大的dp,同时注意钱包的上限
//printf("dp[i-1][j-k] - k*c[i-1] + x为%d\n此时w+j*b为%d, dp[%d][%d]应该为%d\n", dp[i-1][j-k] - k*c[i-1] + x,w+j*b, i, j, min(dp[i-1][j-k] - k*c[i-1] + x, w + j * b));
}
//printf("dp[%d][%d] = %d, sum = %d\n", i, j, dp[i][j], sum);
} }
ll ans;
for(int i = 0; i <= sum; i++)
if(dp[n][i] != -1) ans = i;
printf("%lld\n", ans);
return 0;
}

Codeforces 922 E Birds (背包dp)被define坑了的一题的更多相关文章

  1. Codeforces 864E Fire(背包DP)

    背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...

  2. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  3. Codeforces 730J:Bottles(背包dp)

    http://codeforces.com/problemset/problem/730/J 题意:有n个瓶子,每个瓶子有一个当前里面的水量,还有一个瓶子容量,问要把所有的当前水量放到尽量少的瓶子里至 ...

  4. Codeforces 946 课程表背包DP 数位DFS构造

    A B 给你A,B 两个数      1.a=0 OR b=0 break      2.a>=2b a=a-2b        3.b>=2a b=b-2a 如果只是单纯模拟肯定会超时 ...

  5. Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 背包dp

    D. Yet Another Subarray Problem You are given an array \(a_1, a_2, \dots , a_n\) and two integers \( ...

  6. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  7. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

  8. luogu 4377 Talent show 01分数规划+背包dp

    01分数规划+背包dp 将分式下面的部分向右边挪过去,通过二分答案验证, 注意二分答案中如果验证的mid是int那么l=mid+1,r=mid-1,double类型中r=mid,l=mid; 背包dp ...

  9. bzoj1625:[Usaco2007 Dec]宝石手镯(背包dp板子)

    1625: [Usaco2007 Dec]宝石手镯 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1349  Solved: 954[Submit][St ...

随机推荐

  1. 使用JavaMail发送邮件(含文本/附件/图片)的工具类

    记录利用公司内搭建的smtp邮件服务器,使用javax mail发送邮件的程序 package com.test.mailTest; import java.util.Date; import jav ...

  2. 投票:OAuth2.0 技术选型你会怎么选

    1. 前言 在使用 OAuth2.0 中 Authorization Server (授权服务器)是一个回避不了的设施,在大多数情况下我们调用的是一些知名的.可靠的.可信任的第三方平台,比如 QQ.微 ...

  3. OOP之重载

    PHP中的重载指的是动态的创建属性与方法,是通过魔术方法来实现的. 属性的重载通过调用类中的__set,__get,__isset,__unset方法来分别实现对不存在属性的赋值.读取.判断属性是否设 ...

  4. input 只允许输入小数

    oninput = "value=value.replace(/[^\d]/g,'')" 输入浮点数不好使 突发奇想自己写一个与众不同的... oninput="valu ...

  5. sin 与 cos 的用法

    这两个函数使用过程中要,如 sin(x) , x 在这里表示的是弧度,至于弧度要如何计算呢 ? pi / 180 = 弧度 / 角度 内部的参数可以整数或者浮点数,以前面做过的一道题而言,大量的调用函 ...

  6. 150行代码打造.net core生产力工具,你值得拥有

    你是否在初学 .net core时,被依赖注入所折磨? 你是否在开发过程中,为了注入依赖而不停的在Startup中增加注入代码,而感到麻烦? 你是否考虑过或寻找过能轻松实现自动注入的组件? 如果有,那 ...

  7. 让现有vue前端项目快速支持多语言 - 用.net core程序快速替换中文为资源Key,咱不干体力活

    前言 最近应公司上层要求,需要将现有项目尽快支持多语言,而中文内容可以找专业人员翻译.那么咱们说干就干,首先我们项目的前端是用vue写的spa程序且组件方面用的element ui,那么自然而然想到用 ...

  8. 8.JavaSE之变量、常量、作用域

    变量variable: 变量是什么:就是内存中开辟的可以变化的量! Java是一种强类型语言,每个变量都必须声明其类型. Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型,作用域   ...

  9. [DP][SA][可持久化线段树]黑红兔

    源自 xyz32768 菜鸡的 FJ 省冬令营模拟赛题 原题 CF1063F Statement 给定一个长度为 \(n\) 的字符串 \(s\),仅包含小写英文字母 要从中从左往右选出若干段不相交的 ...

  10. visul studio 使用git扫盲帖。

    写给和一样的菜B 有必要知道的命令: git rm --cached (文章底部有git命令大全) 创建.gitignore文件 windows版: 在项目根目录下面创建gitignore.txt文件 ...