Dream City

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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 ai coins 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.

 
题目意思:
有n颗树,每棵树都有固定的初始值,和有增长值,
给出m天,每天只能砍一棵树,那么对与第j天,
如果选择砍掉第i棵树,
那么收获的硬币就是初始值+增长值*(j-1),
怎样砍才能使收获的硬币最多
解题思路:增长快的要放在后面砍会比较有利。
在做背包前,要先排个序,
可将问题转化为01背包问题。
dp[i][j]表示前i课树,第j天所能取到的最大值。
状态转移方程 :
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+p[i].v1+p[i].v2*(j-1));
 
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define eps 10e-6
#define INF 999999999
using namespace std;
#define max_v 255
struct node
{
int v1;
int v2;
}p[max_v];
bool cmp(node a,node b)
{
return a.v2<b.v2;
}
int dp[max_v][max_v];
int main()
{
int t;
int n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m); for(int i=;i<=n;i++)
{
scanf("%d",&p[i].v1);
}
for(int i=;i<=n;i++)
{
scanf("%d",&p[i].v2);
} sort(p+,p++n,cmp); memset(dp,,sizeof(dp)); int ans=-INF;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
dp[i][j]=max(dp[i-][j],dp[i-][j-]+p[i].v1+p[i].v2*(j-));
ans=max(ans,dp[i][j]);
}
}
printf("%d\n",ans);
}
return ;
}
/* 题目意思:
有n颗树,每棵树都有固定的初始值,和有增长值,
给出m天,每天只能砍一棵树,那么对与第j天,
如果选择砍掉第i棵树,
那么收获的硬币就是初始值+增长值*(j-1),
怎样砍才能使收获的硬币最多 解题思路:增长快的要放在后面砍会比较有利。
在做背包前,要先排个序,
可将问题转化为01背包问题。
dp[i][j]表示前i课树,第j天所能取到的最大值。
状态转移方程 :
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+p[i].v1+p[i].v2*(j-1)); */

ZOJ3211-Dream City(贪心思想+变形的01背包)的更多相关文章

  1. 3466 ACM Proud Merchants 变形的01背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...

  2. 背包问题(01背包,完全背包,多重背包(朴素算法&&二进制优化))

    写在前面:我是一只蒟蒻~~~ 今天我们要讲讲动态规划中~~最最最最最~~~~简单~~的背包问题 1. 首先,我们先介绍一下  01背包 大家先看一下这道01背包的问题  题目  有m件物品和一个容量为 ...

  3. 01背包--hdu2639

    hdu-2639 The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rooki ...

  4. 【牛客-14602】xinjun与阴阳师(01背包)

    xinjun与阴阳师 题目描述 xinjun是各类手游的狂热粉丝,因随手一氪.一氪上千而威震工大,现在他迷上了阴阳师.xinjun玩手游有一个习惯,就是经过层层计算制定出一套方案来使操作利益最大化(因 ...

  5. HDU 3446 有贪心思想的01背包

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  6. HDU -2546饭卡(01背包+贪心)

    这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...

  7. 0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论

    一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i ...

  8. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  9. hdu–2369 Bone Collector II(01背包变形题)

    题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...

随机推荐

  1. AIX修改时区,配置NTP服务

    AIX修改时区 smitty --> System Environments -->Change/Show Data and Time -->Change Time Zone Usi ...

  2. React—Native开发之原生模块向JavaScript发送事件

    首先,由RN中文网关于原生模块(Android)的介绍可以看到,RN前端与原生模块之 间通信,主要有三种方法: (1)使用回调函数Callback,它提供了一个函数来把返回值传回给JavaScript ...

  3. Spring Boot—03REST请求

    package com.smartmap.sample.ch1.controller.rest; import java.util.List; import org.apache.commons.lo ...

  4. Git Flow 代码版本控制模型

    说到代码版本控制,推荐一下最新的Git.跟SVN相比,最大的区别是它在本地也保存了一个代码库,这样可以离线工作,首先将代码提交到本地仓库,联网之后再同步到服务器端.代码托管网站 Github 和 Bi ...

  5. jQuery轮播图(手动点击轮播)

    下面来看看最终做的手动点击轮播效果: 一.原理说明 (1)首先是轮播图的架构,我采用了一个最外边的大div包住两个小div,一个小div里面放四张图片,另一个小div里面放四个数字按钮 (2)对最外边 ...

  6. 五种常用web服务器jvm参数设置

     一.tomcat Tomcat默认可以使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,需要调大.有以下几种方法可以选用:第一种方法:在配置文件中设置Windows下,在文件/bi ...

  7. js获取当前页面url信息方法(JS获取当前网址信息)

    设置或获取对象指定的文件名或路径. alert(window.location.pathname) 设置或获取整个 URL 为字符串. alert(window.location.href); 设置或 ...

  8. 'webpack' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    npm updatea -g 很严重,把本地npm安装包都更新了,跟项目npm安装包版本不一,导致 意思是版本冲突,手动卸载了,重新安装还是最新版本,很是头疼.找同事的电脑拷贝了一份,然后复制过来报“ ...

  9. leetCode题解之旋转数字

    1.题目描述 X is a good number if after rotating each digit individually by 180 degrees, we get a valid n ...

  10. 5.String StringBuffer StringBuilder

    String,StringBuffer和StringBuilder三者的讲解 对于StringBuffer和StringBuilder是对Stirng的一个优化. 之前已经说过了,String对象一旦 ...