ZOJ 3211 Dream City (J) DP

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

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.

题意:给出N棵树 a[i]表示这棵树初始价值, b[i]表示每天能够增加多少价值。每天必须要砍树,问过M天后能得到的最大价值。

思路:有顺序的DP

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <string>
 5 #include <iomanip>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <vector>
 9 #include <map>
10 using namespace std;
11 #define maxn 255
12 int T, N, M;
13 int dp[maxn][maxn];
14 struct Node{
15     int a, b;
16 }node[maxn];
17 int Max(int a, int b){
18     return a>b?a:b;
19 }
20 bool cmp(Node x, Node y){
21     return x.b < y.b;
22 }
23 int main(){
24     scanf("%d", &T);
25     while(T--){
26         scanf("%d%d", &N, &M);
27         for(int i = 1; i <= N; i++) scanf("%d", &node[i].a);
28         for(int i = 1; i <= N; i++) scanf("%d", &node[i].b);
29         memset(dp, 0, sizeof(dp));
30         sort(node+1, node+1+N, cmp);
31         for(int i = 1; i <= N; i++){
32             for(int j = 1; j <= M; j++){
33                 dp[i][j] = Max(dp[i-1][j], dp[i-1][j-1] + node[i].a + node[i].b*(j-1));
34             }
35         }
36         printf("%d\n", dp[N][M]);
37
38     }
39
40     return 0;
41 }

ZOJ 3211 Dream City (J) DP,布布扣,bubuko.com

时间: 2024-07-30 00:49:56

ZOJ 3211 Dream City (J) DP的相关文章

ZOJ 3211 Dream City(线性DP)

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 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 aicoins o

ZOJ 3211 Dream City DP 01背包 经典问题

题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上每天回结出金子.已经有n棵树,JAVAMAN要停留m天,每天只能砍掉一棵树,砍掉树后就能得到树上的黄金.给定n棵树上原有的黄金a[i]和每天可以新增加的黄金b[i],求他最多可以得到多少黄金.中途如果有1天不砍树的话,之后的日子久不能砍树,所有最好每天都砍树,或者直到树被砍完. 这个其实是个背包问题,把日期看成背包的容量.然后n棵树当成n个物品. 假设我们取得由某 m 棵树组成的最优解.如果先砍的树的b值比后砍的树的b值大, 那么我们

zoj 3211 - Dream City

题目:javaman来到了一个城市,这里有很多长着金币的树,每棵树每晚还会结出新的金币, 现在他每天白天只能砍一棵树,最多在这里呆m天,求能得到的最大金币数. 分析:贪心+dp,二维01背包.如果砍树的集合确定,那一定按照b递增的顺序砍,因此排序后背包. 说明:(2011-11-02 05:49). #include <stdio.h> #include <stdlib.h> #include <string.h> #define max(a,b) ((a)>(b

Dream City(线性DP)

描述 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

ZOJ 3450 Doraemon&#39;s Railgun (DP&#183;分组背包)

题意  多啦A梦有一个超电磁炮  然后要打死n堆敌人  在同一条射线上的敌人只有先打死前面的一堆才能打后面的一堆  给你打死某堆敌人需要的时间和这堆敌人的人数   问你在T0时间内最多打死多少个敌人 分组背包问题  先要把同一条射线上的敌人放到一个分组里  后面的敌人的时间和人数都要加上前面所有的  因为只有前面的都打完了才能打后面的  然后每组最多只能选择一个   判断共线用向量处理   然后去背包就行了 注意给你的样例可能出现t=0的情况   在分组时需要处理一下    被这里卡了好久 #i

POJ 2411 Mondriaan&#39;s Dream(状压DP)

http://poj.org/problem?id=2411 求一个n*m矩阵用1*2方块去填满的情况有几种 思路:状压dp,先预处理那些状态之间能互相到达,情况就几种,上一个两个1,下一个状态也两个1,上一个为0,下一个必须为1,还有一种是上一个为1,下一个为0的情况 然后就一层层往后递推即可 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int

POJ 2411 &amp;&amp; HDU 1400 Mondriaan&#39;s Dream (状压dp 经典题)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12341   Accepted: 7204 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

zoj 3791 An Easy Game dp

An Easy Game Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Edward and Flandre play a game. Flandre will show two 01-strings s1 and s2, the lengths of two strings are n. Then, Edward must move exact k steps. In each step, Edward should ch

POJ 2411 Mondriaan&#39;s Dream ——状压DP 插头DP

[题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出来. 加了点优(gou)化(pi),然后poj上1244ms垫底. 大概的方法就是考虑每一层横着放的情况,剩下的必须竖起来的情况到下一层取反即可. 然后看了 <插头DP-从入门到跳楼> 这篇博客,怒抄插头DP 然后16ms了,自己慢慢YY了一下,写出了风(gou)流(pi)倜(bu)傥(tong)