【ZOJ - 3211】Dream City (01背包类问题,贪心背包)

题干:

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 mor less days from the first day!)

Given nmai 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

Hint

s:
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.

解题报告:

本来思考:定义的状态是:dp[i][j] 第i天砍了第j棵树,但是这个肯定不对,以为看不到之前哪些砍过哪些没砍。dp[i][j]代表第i填砍了前j棵树,你i天肯定砍了i棵树,那么剩下的肯定都从后面砍的,所以涉及到一个排序问题,而且这个状态定义会有后效性,所以pass掉。

正解:dp[i][j]代表考虑对于第i棵树,总共砍了j棵树(或者砍了j天),的最大价值,那么此时有选和不选两种状态,分别转移。问题转化成:也就是我有一个体积为m的背包,n个物品,每个物品体积为1,并且物品的价值随放入顺序而改变,问你最优放入顺序(最大价值)。

那么这种问题肯定要贪心排序,处理掉“物品的价值会随时间改变而改变” 这一要素,对于这题假设我们选了m种物品,那么肯定b越大的越后选。所以我们按照b排序。这样就是n个物品选m个物品的最优解问题了,01背包解决。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 500 + 5;int n,m;
set<int> ss[MAX][MAX];
ll val[MAX][MAX];//第i棵树 第j天  的val 
struct Node {int a,b;bool operator<(const Node c)const{return b < c.b;}
} node[MAX];
ll dp[MAX];
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);memset(dp,-0x3f,sizeof dp);for(int i = 1; i<=n; i++) scanf("%d",&node[i].a);for(int i = 1; i<=n; i++) scanf("%d",&node[i].b);memset(val,0,sizeof val);sort(node+1,node+n+1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(j == 1) val[i][j] = node[i].a;else val[i][j] = val[i][j-1] + node[i].b;}}dp[0]=0;for(int i = 1; i<=n; i++) {for(int j = m; j>=1; j--) {dp[j] = max(dp[j],dp[j-1] + val[i][j]);}}printf("%lld\n",dp[m]);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440636.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gitlable iphone_使用gitlab ci构建IOS包并发送通知消息到企业微信

在之前的文章中&#xff0c;我们介绍了使用gitlab ci构建Android包的方法。今天我们介绍使用gitlab ci如何构建IOS包&#xff0c;并且在打包成功或者失败时&#xff0c;如何将消息通知到企业微信。如果对gitlab ci还不熟悉的&#xff0c;可以参考之前的文章使用gitlab ci构建An…

2020计算机网络基础与应用,2020年国防科技大学硕士研究生考试F1107计算机网络基础考试大纲...

全国各省市院校2020年硕士研究生考试大纲汇总(持续更新中)》》》2020年全国硕士研究生入学考试命题标准大纲已于7月8日正式公布&#xff0c;接下来全国各研招院校将陆续发布2020考研专业课大纲。以下是中公考研小编整理的“2020年国防科技大学硕士研究生考试F1107计算机网络基础…

【ZOJ - 3212 】K-Nice (构造)

题干&#xff1a; This is a super simple problem. The description is simple, the solution is simple. If you believe so, just read it on. Or if you dont, just pretend that you cant see this one. We say an element is inside a matrix if it has four neighbori…

chrome jsp 显示不正常_JSP程序在chrome下不兼容的问题!

比如&#xff0c;我这里有个&#xff1a;">比如&#xff0c;我这里有个&#xff1a;οnclick"javascript:onChooseLongCode()" value"...">在下面的JS动作中&#xff1a;function onChooseLongCode(){var qryFldVal qryFld.value;//要用 var q…

人脑意识转入量子计算机,人脑产生意识:可能是因为量子纠缠

图片来源&#xff1a;quanta magazine一提到“量子意识”这个词语&#xff0c;大多数物理学家都会选择避而不谈&#xff0c;因为这个词语听起来好像有点“民科”&#xff0c;甚至让人联想宗教或者玄学。不过出人意料的是&#xff0c;量子效应可能真的在人类的认知过程中起到了一…

【ZOJ - 1163】The Staircases(dp)

题干&#xff1a; One curious child has a set of N little bricks. From these bricks he builds different staircases. Staircase consists of steps of different sizes in a strictly descending order. It is not allowed for staircase to have steps equal sizes. Ev…

函授本科统考计算机考试时间,函授2006级计算机、机电本科第一学期考试时间安排.doc...

函授2006级计算机、机电本科第一学期考试时间安排.doc函授2006级计算机、机电本科第一学期考试时间安排答疑时间9月16日下午2&#xff1a;30&#xff0d;3&#xff1a;309月16日下午3&#xff1a;30&#xff0d;4&#xff1a;309月16日下午4&#xff1a;30&#xff0d;5&#x…

的级联选择_级联接收机的计算及Y因子噪声因子测量法

对于RF系统工程师来说&#xff0c;计算噪声系数预算对预测产品性能至关重要。一、级联接收机的计算这部分讨论噪声系数的一般概念&#xff0c;以及产品定义和电路设计者如何利用噪声系数表示噪声性能要求。噪声系数也用于预测接收机系统的总体灵敏度。也介绍级联接收机的噪声系…

*【PAT天梯】分而治之(并查集,暴力)

题干&#xff1a; L2-1 分而治之 &#xff08;25 分&#xff09; 分而治之&#xff0c;各个击破是兵家常用的策略之一。在战争中&#xff0c;我们希望首先攻下敌方的部分城市&#xff0c;使其剩余的城市变成孤立无援&#xff0c;然后再分头各个击破。为此参谋部提供了若干打击…

免费测试英语发音的软件,2020英语音标免费版

2020英语音标免费版是一款安卓手机英语音标内容学习软件&#xff0c;在这里汇集了英语音标相关内容学习&#xff0c;包含了音标、发音等内容学习&#xff0c;能够进行对比练习&#xff0c;不断对比&#xff0c;让发音更标准&#xff0c;拥有详细的发音知识可以学习&#xff0c;…

ajax后台重定向会返回什么_跳转,AJAX返回和重定向

## 页面跳转操作完成后,成功或失败页面跳转可以使用 控制器基类的success,error方法:#### success方法php/*** 操作成功跳转的快捷方法* access protected* param mixed $msg 提示信息* param string $url 跳转的URL地址* param mixed $data 返回的数据* param integer $wait 跳…

【PAT天梯】【L2-2 小字辈(左子右兄加强版)】(树,水题)

题干&#xff1a; L2-2 小字辈&#xff08;左子右兄加强版&#xff09; &#xff08;25 分&#xff09; 本题给定一个庞大家族的家谱&#xff0c;要请你给出最小一辈的名单。 需要注意的是&#xff0c;家族成员的输入方式&#xff0c;是按照“Left-child Right-sibling”&…

W10的服务器正在运行,win10开机提示服务器正在运行中的解决教程

今天小编给大家分享的是win10开机提示服务器正在运行中的解决教程。最近有win10的用户反映&#xff0c;在开机的时候莫名的就出现了个“服务器正在运行中”的气泡提示&#xff0c;出现此问题困扰的用户&#xff0c;请来看看下面的解决教程吧。使用win10系统过程中&#xff0c;在…

host ntrip 千寻rtk_最新测量干货:南方银河1和银河1Plus RTK设备连接千寻cors账号的设置方法...

无论刚接触网络RTK的测量员是想要进行华测RTK连接千寻cors账号的操作&#xff0c;还是中海达RTK连接千寻cors账号&#xff0c;亦或者司南RTK连接千寻cors账号的操作&#xff0c;我们在进行cors账号的连接操作之前都应该对操作的RTK设备有一个了解&#xff0c;所以今天我们在给大…

【CodeForces - 1084D】The Fair Nut and the Best Path (树形dp)

题干&#xff1a; The Fair Nut is going to travel to the Tree Country, in which there are nn cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent …

消息测试服务器,测试统一消息服务器功能

测试统一消息服务器功能08/07/2014本文内容适用于&#xff1a; Exchange Server 2007 SP1, Exchange Server 2007上一次修改主题&#xff1a; 2007-08-09在运行 Microsoft Exchange Server 2007 的计算机上安装统一消息服务器角色并启用和配置统一消息 (UM) 之后&#xff0c;可…

python不同曲线设置标签_【图像分类】基于Pascal VOC2012增强数据的多标签图像分类实战...

接着上一次的多标签分类综述&#xff0c;本文主要以Pascal VOC2012增强数据集进行多标签图像分类训练&#xff0c;详细介绍增强数据集制作、训练以及指标计算过程&#xff0c;并通过代码进行详细阐述&#xff0c;希望能为大家提供一定的帮助&#xff01;作者&编辑 | 郭冰洋…

【HDU - 5934】Bomb (强连通分量Tarjan + 缩点)

题干&#xff1a; There are NN bombs needing exploding. Each bomb has three attributes: exploding radius riri, position (xi,yi)(xi,yi) and lighting-cost cici which means you need to pay cici cost making it explode. If a un-lighting bomb is in or on the b…

css光线,css 射线实现方法

四种浏览器对 clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth 和 scrollWidth 的解释差异网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...黄聪&#xff1a;说说JSON和…

表字段注释放在哪_橱柜台面6种材质优缺点大解剖,我来测评,告诉你哪一种适合你!...

很多人装修厨房&#xff0c;只知道橱柜要好用&#xff0c;厨房才好用。但在做橱柜的时候&#xff0c;除了橱柜的选择&#xff0c;很多装修新手还会遇到一些其他疑问&#xff1a;① 橱柜的台面用什么材质的比较好&#xff1f;② 台面的挡水条到底要不要做&#xff1f;③ 橱柜台面…