http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3211Dream City


Time Limit: 1 Second      Memory Limit: 32768 KB

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 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 npositive 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

  1. 2
  2. 2 1
  3. 10 10
  4. 1 1
  5. 2 2
  6. 8 10
  7. 2 3

Sample Output

  1. 10
  2. 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

题意:你可以在m天每天砍掉一棵树,在第Q天砍掉的树w获得的价值=a[w]+b[w]*(Q-1),求能获得的最大价值。

题解:容易想到转移方程为dp[i][j]=max(dp[i][j],dp[i-1][j-1]+p[i].b*(j-1)+p[i].a);,但是由于转移方程中有一个变量的贡献和天数有关即p[i].b,所以可以贪心地想到应该让p[i].b大的排在后面进行更新,所以排序之后进行dp即可

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn=1e4+;
  8. ll dp[][];
  9. struct pot{
  10. ll a;
  11. ll b;
  12. }p[];
  13. bool cmp(struct pot aa,struct pot bb){
  14. return aa.b<bb.b;
  15. }
  16. int main(){
  17. int t;
  18. cin>>t;
  19. while(t--){
  20. int n,m;
  21. scanf("%d%d",&n,&m);
  22. for(int i=;i<=n;i++)scanf("%lld",&p[i].a);
  23. for(int i=;i<=n;i++)scanf("%lld",&p[i].b);
  24. memset(dp,,sizeof(dp));
  25. sort(p+,p++n,cmp);
  26. ll ans=;
  27. for(int i=;i<=n;i++){
  28. for(int j=;j<=m;j++){
  29. dp[i][j]=dp[i-][j];
  30. dp[i][j]=max(dp[i][j],dp[i-][j-]+p[i].b*(j-)+p[i].a);
  31. }
  32. }
  33. printf("%lld\n",dp[n][m]);
  34. }
  35. return ;
  36. }

https://www.luogu.org/problemnew/show/P1417

一共有n件食材,每件食材有三个属性,ai,bi和ci,如果在t时刻完成第i样食材则得到ai-t*bi的美味指数,用第i件食材做饭要花去ci的时间。

众所周知,gw的厨艺不怎么样,所以他需要你设计烹调方案使得美味指数最大

输入格式:

第一行是两个正整数T和n,表示到达地球所需时间和食材个数。

下面一行n个整数,ai

下面一行n个整数,bi

下面一行n个整数,ci

输出格式:

输出最大美味指数

输入样例#1:

  1. 74 1
  2. 502
  3. 2
  4. 47
输出样例#1:

  1. 408

【数据范围】

对于40%的数据1<=n<=10

对于100%的数据1<=n<=50

所有数字均小于100,000

题解:容易想到转移方程为dp[j]=max(dp[j],dp[j-p[i].c]+p[i].a-j*p[i].b);其中两个变量的贡献和天数有关即p[i].b,p[i].c,两种食材设为A,B 则他们排列为AB时的贡献为-(p[A].c+p[B].c)*p[B].b-p[A].c*p[A].b,他们排列为BA时的贡献为-(p[A].c+p[B].c)*p[A].b-p[B].c*p[B].b,当(p[A].c+p[B].c)*p[B].b-p[A].c*p[A].b<-(p[A].c+p[B].c)*p[A].b-p[B].c*p[B].b时,排列为AB比BA更优,化简之后就是p[A].c*p[B].b<p[A].b*p[B].c,所以可以以此排序,然后dp

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<vector>
  6. using namespace std;
  7. typedef long long ll;
  8. ll dp[];
  9. struct pot{
  10. ll a;
  11. ll b;
  12. ll c;
  13. }p[];
  14. bool cmp(struct pot ab,struct pot ba){
  15. return (ab.c*ba.b<ab.b*ba.c);
  16. }
  17. int main(){
  18. int t,n;
  19. scanf("%d%d",&t,&n);
  20. for(int i=;i<=n;i++)scanf("%lld",&p[i].a);
  21. for(int i=;i<=n;i++)scanf("%lld",&p[i].b);
  22. for(int i=;i<=n;i++)scanf("%lld",&p[i].c);
  23. ll ans=;
  24. sort(p+,p++n,cmp);
  25. for(int i=;i<=n;i++){
  26. for(int j=t;j>=p[i].c;j--){
  27. //dp[j]=max(dp[j],dp[j-1]);
  28. dp[j]=max(dp[j],dp[j-p[i].c]+p[i].a-j*p[i].b);
  29. ans=max(ans,dp[j]);
  30. }
  31. }
  32. cout<<ans<<endl;
  33. return ;
  34. }

[洛谷P1417 烹调方案]贪心+dp的更多相关文章

  1. 洛谷P1417 烹调方案【dp】

    题目:https://www.luogu.org/problemnew/show/P1417 题意: 一道菜有$a,b,c$三个值.烧一道菜的时间是$c$.得到的价值是,$a-t*b$其中$t$是菜完 ...

  2. 洛谷 P1417 烹调方案

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  3. 洛谷P1417 烹调方案

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  4. 洛谷 P1417 烹调方案 (01背包拓展)

    一看到这道题就是01背包 但是我注意到价值和当前的时间有关. 没有想太多,直接写,0分 然后发现输入方式不对-- 改了之后只有25分 我知道wa是因为时间会影响价值,但不知道怎么做. 后来看了题解,发 ...

  5. 洛谷 P1417 烹调方案 题解

    题面 这道题是一道典型的排序dp a[i]−b[i]∗(t+c[i])+a[j]−b[j]∗(t+c[i]+c[j]) a[j]−b[j]∗(t+c[j])+a[i]−b[i]∗(t+c[i]+c[j ...

  6. 洛谷 P1417烹调方案

    题目大意: 一共有n件食材,每件食材有三个属性,ai,bi和ci,如果在t时刻完成第i样食材则得到ai-t*bi的美味指数,用第i件食材做饭要花去ci的时间. 求最大美味指数之和. 分析: 显然的0/ ...

  7. 洛谷1417 烹调方案 dp 贪心

    洛谷 1417 dp 传送门 挺有趣的一道dp题目,看上去接近于0/1背包,但是考虑到取每个点时间不同会对最后结果产生影响,因此需要进行预处理 对于物品x和物品y,当时间为p时,先加x后加y的收益为 ...

  8. P1417 烹调方案 背包DP

    题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...

  9. Luogu P1417烹调方案【dp/背包】By cellur925

    题目传送门 我们看到这道题,就会想起背包.于是我就一顿01背包敲,结果发现只有30分.后来看题解发现需要对输入的食材进行排序. 我们回想国王游戏一题,各位大臣的排列顺序会对权值造成影响,所以我们就预先 ...

随机推荐

  1. http协议与浏览器缓存

    http协议与浏览器缓存 F5刷新与在地址栏回车的区别 链接

  2. flask不定参数的传递。多参数,多次传递

    有的时候有一个分类查询,再来一个排序,这就有两个参数要传递多次. 还是不定长度,不定内容的传递. 这个是用request.args来实现: def home(): requests=request.a ...

  3. centos7 新装系统网络配置

    [root@localhost ~]# cat /etc/sysconfig/grub GRUB_TIMEOUT= GRUB_DISTRIBUTOR="$(sed 's, release . ...

  4. Cracking The Coding Interview 3.2

    //How would you design a stack which, in addition to push and pop, also has a function min which ret ...

  5. 在JS文件中,不需要<script>标签

    在JS文件中,不需要<script>标签\

  6. [SCOI2005]栅栏

    这个题...只能说比较水... 排序后,算一个前缀和,二分dfs查找答案...加上两个剪枝就过了...QVQ 我只能刷这种水题...我太菜了...QVQ #include<iostream> ...

  7. DevExpress ASP.NET v18.2新功能详解(二)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Cont ...

  8. htmlayout 最简单的实践,用于理解实现原理。

    / testHtmlayout.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include "testHtmlayout.h&qu ...

  9. Problem B 一元二次方程类

    Description 定义一个表示一元二次方程的类Equation,该类至少具有以下3个数据成员:a.b和c,用于表示方程“a*x*x + b*x +c = 0”.同时,该类还至少具有以下两个成员函 ...

  10. AOP 实现自定义注解

    1.自定义注解2.编写 AOP3.测试 1.自定义注解 package com.base.yun.spring.aop; import java.lang.annotation.Documented; ...