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. BZOJ4636: 蒟蒻的数列(动态开节点线段树)

    题意 题目链接 Sol 直接上动态开节点线段树 因为只有一次询问,所以中途不需要下传标记 #include<bits/stdc++.h> #define LL long long usin ...

  2. jQuery箭头切换图片 - 学习笔记

    jQuery箭头切换图片 布局 3d位移 变形原点 jQuery transform:translate3d(x,y,z):        x 代表横向坐标移向量的长度       y 代表纵向坐标移 ...

  3. AR中的SLAM(二)

    写在前面 本文想讨论一下AR的架构和SLAM在其中的作用. AR AR的框架可以简单划分为感知和交互两部分. 感知部分主要负责信息的收集和处理.信息主要通过不同的传感器收集,包括图像.设备加速度.距离 ...

  4. 使用Callable和Future接口创建线程

    具体是创建Callable接口的实现类,并实现clall()方法.并使用FutureTask类来包装Callable实现类的对象,且以此FutureTask对象作为Thread对象的target来创建 ...

  5. Android Viewpager+Fragment实现滑动标签页

    ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应. 主页布局 <?xml version="1.0" ...

  6. 网站源IP暴露使用高防之后还行不行如何解决?

    如题:使用高防后源站IP暴露的解决办法 在购买高防IP后,如果还存在攻击绕过高防直接打到源站IP的情况,就需要更换下源站IP了.但在这之前,请务必排查确认没有其他可能暴露源站IP的因素后,再去更换源站 ...

  7. javaweb 读取properties配置文件参数

    场景1:在servlet中读取properties配置文件参数 protected void doGet(HttpServletRequest request, HttpServletResponse ...

  8. 解决eclipse 文件更新不自动刷新的问题

    打开eclipse 1. Window ===> Preferences ===> General ===> Workspace 2. 勾选 1> Refresh using ...

  9. 铁乐学python_Day38_多进程和multiprocess模块1

    铁乐学python_Day38_多进程和multiprocess模块1 [进程] 运行中的程序就是一个进程. 所有的进程都是通过它的父进程来创建的. 因此,运行起来的python程序也是一个进程,那么 ...

  10. Docker 命令总结

    1 启动镜像 docker run -i -t centos /bin/bash