ZOJ 3211 Dream City(线性DP)
Dream City
Time Limit: 1 Second Memory Limit: 32768 KB
JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has aicoins on it (i=1, 2, 3...n). Surprisingly, each tree i can grow bi new coins each day if it is not cut down. From the first day, JAVAMAN can choose to cut down one tree each day to get all the coins on it. Since he can stay in the Dream City for at most m days, he can cut down at most m trees in all and if he decides not to cut one day, he cannot cut any trees later. (In other words, he can only cut down trees for consecutive m or less days from the first day!)
Given n, m, ai and bi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.
Input
There are multiple test cases. The first line of input contains an integer T (T <= 200) indicates the number of test cases. Then T test cases follow.
Each test case contains 3 lines: The first line of each test case contains 2 positive integers n and m (0 < m <= n <= 250) separated by a space. The second line of each test case contains n positive integers separated by a space, indicating ai. (0 < ai <= 100, i=1, 2, 3...n) The third line of each test case also contains n positive integers separated by a space, indicating bi. (0 < bi <= 100, i=1, 2, 3...n)
Output
For each test case, output the result in a single line.
Sample Input
2
2 1
10 10
1 1
2 2
8 10
2 3
Sample Output
10
21
Hints:
Test case 1: JAVAMAN just cut tree 1 to get 10 gold coins at the first day.
Test case 2: JAVAMAN cut tree 1 at the first day and tree 2 at the second day to get 8 + 10 + 3 = 21 gold coins in all.
Author: CAO, Peng
Source: The 6th Zhejiang Provincial Collegiate Programming Contest
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node
{
int s,grow;
}a[];
int t,n,m;
int dp[][];
bool cmp(node a,node b)
{
return a.grow<b.grow;
} int main()
{
while(~scanf("%d",&t))
{
for(;t>;t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i].s);
for(int i=;i<=n;i++)
scanf("%d",&a[i].grow);
sort(a+,a+n+,cmp);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)//dp[i][j]表示到第i棵树为止,砍了j棵获得的最多金币
for(int j=;j<=m;j++)
dp[i][j]=max(dp[i][j],max(dp[i-][j],dp[i-][j-]+a[i].s+(j-)*a[i].grow)); printf("%d\n",dp[n][m]);
}
}
return ;
}
ZOJ 3211 Dream City(线性DP)的更多相关文章
- ZOJ 3211 Dream City(DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3374 题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上 ...
- ZOJ 3211 Dream City DP 01背包 经典问题
题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上每天回结出金子.已经有n棵树,JAVAMAN要停留m天,每天只能砍掉一棵树,砍掉树后就能得到树上的黄金.给定n棵树上原有的黄金a[i]和每 ...
- ZOJ 3211 Dream City
贪心,$dp$. 假设我们知道要选择哪些物品,那么这些物品应该按什么顺序选择呢? 物品$A(a1,b1)$,物品$B(a2,b3)$. 假设物品$A$在第$x$天被选择,物品$B$在第$y$天被选择. ...
- Dream City(线性DP)
描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- POJ-2346 Lucky tickets(线性DP)
Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3298 Accepted: 2174 Descrip ...
- Hills——一道转移方程很“有趣”的线性DP
题目描述 Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlastin ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- 教你高速入门Excel-宏与VBA(续)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/small_baby01/article/details/24028407 引言 通过 ...
- python16_day07【class】
一.初识类 1.类的两种作用:属性引用和实例化 class Garen: #定义英雄盖伦的类,不同的玩家可以用它实例出自己英雄; camp='Demacia' #所有玩家的英雄(盖伦)的阵营都是Dem ...
- python全栈开发从入门到放弃之字典的应用
1.存值 info_dic={'name':'egon','age':18,'sex':'male'} info_dic['job']='IT' #根据key来存值 print(info_dic) 输 ...
- QQ空间动态内容,好友信息,点赞爬虫脚本
一.安装基础的软件包: 1.准备好火狐浏览器,并下载geckodriver,将geckodriver加入到环境变量:下载geckodriver的地址:https://pan.baidu.com/s/1 ...
- mysql数据库存储过程数据迁移案例与比较
cursor 与 insert ...select 对比: cursor:安全,不会造成死锁,可以在服务运行阶段跑,比较稳定. insert...select :速度快,但是可能造成死锁,相比curs ...
- 144. Binary Tree Preorder Traversal (二叉树前序遍历)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Qt5.5.1移植到freescale imx6
一.环境 HOST:ubuntu12.04-LTS Embedded:freescale imx6 linux-3.0.35 CROSS_COMPILE:freescale提供的gcc-4.6.2-g ...
- 哆啦A梦里的某一首诗
<哆啦A梦>长篇<宇宙开拓>主题曲<放松心情>作词:武田铁矢 我终于发现, /昨日失去的一切, /虽然还无法完全意会, /可是对我却非常重要. /加紧脚步追赶, / ...
- winform webbrowser禁用网页target=blank
/// <summary> /// 屏蔽target=_blank 的弹出窗口 /// </summary> /// <param name="sender&q ...
- Spring注解(赋值相关)
上面是与生命周期有关的内容,下面是属性赋值相关的: @Configuration public class ProperTyValueConfig { @Bean public Person pers ...