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. [ios]自定义UI

    参考:http://blog.sina.com.cn/s/blog_7b9d64af0101edqf.html 回忆一下,这么个场景. 我们在一个界面上,要排列多个相同的元素.你马上就可以想到: 1. ...

  2. 动态规划-Largest Sum of Averages

    2018-07-12 23:21:53 问题描述: 问题求解: dp[i][j] : 以ai结尾的分j个部分得到的最大值 dp[i][j] = max{dp[k][j - 1] + (ak+1 + . ...

  3. Hibernate实例二

    Hibernate实例二 一.测试openSession方法和getCurrentSession方法 hebernate中可以通过上述两种方法获取session对象以对数据库进行操作,下面的代码以及注 ...

  4. 雷林鹏分享:Ruby 哈希(Hash)

    Ruby 哈希(Hash) 哈希(Hash)是类似 "employee" => "salary" 这样的键值对的集合.哈希的索引是通过任何对象类型的任意键 ...

  5. WPF中为窗体设置背景图片

    在WPF应用程式中,我们往往想为一个窗体设置一个中意的背景图,而不是单独的为这个Background设置成某种颜色或渐变颜色的背景. 在WPF 利用Expression Blend工具如何达到这种效果 ...

  6. Mac安装软件时 提示已损坏的解决方法

    进入终端: sudo spctl --master-disable

  7. 4-2 什么是WebSocket; Action Cable的使用。Rails guide-6.3视频教学,没有看!

    WebSocket WebSocket是一种在单个TCP连接上进行全双工通讯的协议.WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范.WebSock ...

  8. After reading a picture than out a picture

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  9. ajax中文乱码问题的总结

    ajax中文乱码问题的总结 2010-12-11 22:00 5268人阅读 评论(1) 收藏 举报 ajaxurljavascriptservletcallback服务器 本章解决在AJAX中常见的 ...

  10. spring cloud shutdown graceful 优雅停机

    spring cloud shutdown graceful 优雅停机 当一个服务启动后,会注册到eureka中,其他的服务也可以从eureka获取到新注册的服务.但当我们要停止一个服务的时候,如果直 ...