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 aren trees in the yard. Let's call them tree 1, tree 2 ...and treen. At the first day, each treei hasai coins on it (i=1, 2, 3...n). Surprisingly, each treei can growbi 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 mostm days, he can cut down at mostm 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 consecutivem or less days from the first day!)

Given n,m,ai andbi (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 integerT (T <= 200) indicates the number of test cases. ThenT test cases follow.

Each test case contains 3 lines: The first line of each test case contains 2 positive integersn andm (0 <m <=n <= 250) separated by a space. The second line of each test case containsn positive integers separated by a space, indicatingai. (0 <ai <= 100,i=1, 2, 3...n) The third line of each test case also containsn positive integers separated by a space, indicatingbi. (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.

1,排序问题?按a和b的什么关系排?好像不行

2,贪心?试试。于是得到下面这个错误代码


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<map>
#include<memory.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct in
{
int a,b,c;
bool used;
}L[];
bool cmp(in a,in b)
{
if(a.c==b.c) return a.b>b.b;
return a.c>b.c;
}
long long ans;
int n,m;
void _in()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&L[i].a);
for(int i=;i<=n;i++){
scanf("%d",&L[i].b);
L[i].used=false;
}
}
void _solve()
{
for(int i=m;i>=;i--)
{
for(int j=;j<=n;j++){
if(!L[j].used) L[j].c=L[j].a+L[j].b*(i-);
else L[j].c=;
}
sort(L+,L+n+,cmp);
ans+=L[].c;
L[].used=true;
}
printf("%lld\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
ans=;
_in();
_solve();
}
return ;
}

 

因为考虑到a小的b可能大,会影响到后面的选择,于是此贪心是的思想是从后向前。但错误样例:

a:1 100

b:99 1

如果倒着选(100+1)+1则小于 (99+1)+100;

但是之前做过斜率问题的司机们应该知道这道题的后续性不是倒着选来控制,而是按斜率排序来操作(词穷了)。

具体的:

斜率小的物品,如果在某个阶段不选,那么在以后都不会再选。因为它增长得慢,如果在开始的时候没选它,那到后面别人长得比它快就更轮不到它了。

然后,可将问题转化为01背包问题。dp[i][j]表示前i课树,第j天所能取到的最大值。

状态转移方程 : dp[i][j] = max(dp[i-1][j], dp[i-1][j-1] + tree[i].a + (j-1)*tree[i].b)// (tree[i].a为初始值,tree[i].b为每天增加的价值)

(贪心策略是斜率,算不上真正的斜率优化DP)


#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int dp[][]; //前i课树,第j天所能取到的最大值
struct in
{
int a,b;
}tree[];
bool cmp(in x,in y){ return x.b < y.b; };
int main(){
int t;
t=_S();
while(t--)
{
int n,m;
n=_S();
m=_S();
for (int i = ; i <= n; i++) tree[i].a=_S();
for (int i = ; i <= n; i++) tree[i].b=_S();
sort(tree + ,tree + n + ,cmp); //dp是算出取哪几颗树,但是这几棵树的砍得顺序不一定。砍得顺序不同,导致结果不同。所以先处理好顺序。
for (int i = ; i <= n; i++)
{
for (int j = ; j <= min(i,m); j++) //看网上很多代码都是算到m其实到min(i,m)就可以。
{
dp[i][j] = max(dp[i - ][j],dp[i - ][j - ] + tree[i].a + tree[i].b * (j - )); //第i棵树取或者不取
}
}
printf("%d\n",dp[n][m]);
}
return ;
}

ZOJ 3211dream city dp(效率优化)的更多相关文章

  1. HDU3480_区间DP平行四边形优化

    HDU3480_区间DP平行四边形优化 做到现在能一眼看出来是区间DP的问题了 也能够知道dp[i][j]表示前  i  个节点被分为  j  个区间所取得的最优值的情况 cost[i][j]表示从i ...

  2. 动态规划DP的优化

    写一写要讲什么免得忘记了.DP的优化. 大概围绕着"是什么","有什么用","怎么用"三个方面讲. 主要是<算法竞赛入门经典>里 ...

  3. 【BZOJ-4518】征途 DP + 斜率优化

    4518: [Sdoi2016]征途 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 230  Solved: 156[Submit][Status][ ...

  4. 【BZOJ-3437】小P的牧场 DP + 斜率优化

    3437: 小P的牧场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 705  Solved: 404[Submit][Status][Discuss ...

  5. 【BZOJ-1010】玩具装箱toy DP + 斜率优化

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8432  Solved: 3338[Submit][St ...

  6. php程序效率优化的一些策略小结

    php程序效率优化的一些策略小结   1.在可以用file_get_contents替代file.fopen.feof.fgets等系列方法的情况下,尽量用 file_get_contents,因为他 ...

  7. jquery选择器效率优化问题

    jquery选择器效率优化问题   jquery选择器固然强大,但是使用不当回导致效率问题: 1.要养成将jQuery对象缓存进变量的习惯 //不好的写法 $('#btn').bind("c ...

  8. php性能效率优化

    [size=5][color=Red]php性能效率优化[/color][/size] 最近在公司一边自学一边写PHP程序,由于公司对程序的运行效率要求很高,而自己又是个新手,一开始就注意程序的效率很 ...

  9. Jenkins Kubernetes Slave 调度效率优化小记

    Jenkins K8S Slave 调度效率优化 by yue994488@126.com 使用kubernetes为测试工具Gatling进行大规模压测,压测期间发现Jenkins调度压测实例较慢, ...

随机推荐

  1. Centos修改系统语言

    使用man page帮助时,发现居然是中文的,不过想想即便英语再水,也要逼着自己去适应.于是百度找了一下修改系统语言的方法. 首先使用 locale 命令查看当前的系统语言 然后修改时一般有两种方法, ...

  2. 【Golang 接口自动化05】使用yml管理自动化用例

    我们在前面几篇文章中学习怎么发送数据请求,怎么处理解析接口返回的结果,接下来我们一起来学习怎么进行测试用例管理,今天我们介绍的是使用yml文件进行用例管理,所以首先我们一起来了解一下YAML和它的简单 ...

  3. Codeforces 909C - Python Indentation

    909C - Python Indentation 思路:dp. http://www.cnblogs.com/Leohh/p/8135525.html 可以参考一下这个博客,我的dp是反过来的,这样 ...

  4. 探索Bioconductor数据包

    参考: R的bioconductor包TxDb.Hsapiens.UCSC.hg19.knownGene详解 Bioconductor的数据包library(org.Hs.eg.db)简介

  5. laravel command

    (1) 新建一个command类,并在command类里面写相应的执行函数 其中变量act就是指函数名,handle里面会先判断该函数是不是存在,如果存在就执行,如果不存在就提示函数不存在 class ...

  6. 关于floyd 打印路径的问题

    我们令    f[i][j]  表示从 i-->j的最短路上j前面的那个点. 显然初始化时  f[i][j]=i;  (这样的话先判断一下i是否能到达j好点) 更新条件时,当发现通过点k能使最短 ...

  7. UVA-11029 Leading and Trailing

    Apart from the novice programmers, all others know that you can’t exactly represent numbers raised t ...

  8. 写入CSS的3种方式

    CSS能让网页制作者有效的定制.改善网页的效果. CSS是对HTML的补充(网页设计师曾经为无法很好的控制页面的显示效果而倍感苦恼,CSS的出现解决了这个问题) CSS实现了网页内容和页面效果的彻底分 ...

  9. 守护进程的创建(syslog函数)

    守护进程(daemon)是指在后台运行的,没有控制终端与之相连的进程.它独立于控制终端,通常周期性的执行某种任务. 守护进程是一种很有用的进程.Linux的大多数服务器就是用守护进程的方式实现的,如I ...

  10. 自定义oncontextmenu

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...