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. 让iframe可编辑

    function EnableEdit() { var editor; editor = document.getElementById("HtmlEdit").contentWi ...

  2. Https之SSL原理

    一.HTTPS和SSL HTTP(Hyper TEXT Transfer Protocol超文本传输协议)是目前互联网上应用最为广泛的一种网络协议,用于在Web浏览器和网站服务器之间传递信息,但是HT ...

  3. js二分查找树实现

    function BinaryTree() { var Node = function(key) { this.key = key; this.left = null; this.right = nu ...

  4. parseInt OR Number进行数字的转换

    在js中,字符串转为数字类型是比较常见的,平时用的比较多的是parseFloat和parseInt这两个方法.当然,除了这个方法之外还有一个Number:都是转为数字类型,有什么差别? 可以简单的说N ...

  5. Oracle EBS 请求添加SQL语句

  6. npm run dev时报错“events.js:160 throw er; // Unhandled 'error' event”

    经查,此问题由端口占用导致,node服务器默认端口8080已被其他程序占用,关闭占用端口的程序或者修改node服务器的默认端口即可解决此问题

  7. 退出全屏监听ESC事件

    fullscreenchange事件 fullscreenchange:当窗口大小改变时触发 isFullscreen:全局变量 window.addEventListener("fulls ...

  8. TableView的cell加载倒计时重用问题解决方案

    TableView的cell加载倒计时重用问题解决方案 效果 说明 1. 写过类似需求的朋友一定知道,TableView上面加载倒计时功能会遇到复杂的重用问题难以解决 2. 本人提供一种解决思路,高效 ...

  9. 编写带有点击特效的UIButton

    编写带有点击特效的UIButton 效果: 源码: // // ViewController.m // Button // // Created by XianMingYou on 15/1/18. ...

  10. Imagex用法

    imagex用来合并/导出wim映像实例 查看wim中包含的所有单个wim实例信息,注意其中的index是唯一的,用来区分不同的wim,导出的时候也通过该index导出相应的wimimagex /in ...