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. Thunder团队项目视频展示

    视频链接:http://v.youku.com/v_show/id_XMzA5MjMzMzcyMA==.html?spm=a2h3j.8428770.3416059.1 视频简介:通过一个小情景开篇, ...

  2. 《剑指offer》第十六题(数值的整数次方)

    // 面试题:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需要考 ...

  3. McAfee 与 360使用感受

    运维给配的 win 10.4G内存 ,装的 McAfee,每次开机啥事不干内存去了55%, 开齐qq.微信.vscode.浏览器就要冲90%,再多开几个网页电脑就卡得很, 最近,卡死机了.. 然后看了 ...

  4. Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  5. angular编译机制

    转载https://segmentfault.com/a/1190000011562077 Angular编译机制 前言 http://www.cnblogs.com/ztwBlog/p/620975 ...

  6. 雷林鹏分享:C# 泛型(Generic)

    C# 泛型(Generic) 泛型(Generic) 允许您延迟编写类或方法中的编程元素的数据类型的规范,直到实际在程序中使用它的时候.换句话说,泛型允许您编写一个可以与任何数据类型一起工作的类或方法 ...

  7. python-GUI,生成ssn

    第一次做这个, 样子有点丑,主要是实现功能,做测试的时候,经常要用到身份证号.手机号.姓名等,这里先生成ssn,后续研究怎么做成客户端 代码: from tkinter import * from u ...

  8. 012 - jstat命令查看jvm的GC情况 | jvm

    jstat命令可以查看堆内存各部分的使用量,以及加载类的数量. 命令的格式如下: jstat -<option> [-t] [-h<lines>] <vmid> [ ...

  9. android webservice 成功版(帅哥)

    package com.ts.xtweatherreport01;import java.io.UnsupportedEncodingException; import android.annotat ...

  10. BZOJ 1601 [Usaco2008 Oct]灌水 (最小生成树)

    题意 Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库. 建造一个水库需要 ...