博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ 3211 Dream City (J) DP
阅读量:5256 次
发布时间:2019-06-14

本文共 2701 字,大约阅读时间需要 9 分钟。

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

 

22 110 101 12 28 102 3

 

Sample Output

 

1021

 

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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std;11 #define maxn 25512 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 }

 

转载于:https://www.cnblogs.com/titicia/p/3917087.html

你可能感兴趣的文章
网站产品设计
查看>>
代理ARP
查看>>
go 学习笔记(4) ---项目结构
查看>>
java中静态代码块的用法 static用法详解
查看>>
Java线程面试题
查看>>
Paper Reading: Relation Networks for Object Detection
查看>>
day22 01 初识面向对象----简单的人狗大战小游戏
查看>>
mybatis源代码分析:深入了解mybatis延迟加载机制
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
提高PHP性能的10条建议
查看>>
svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法...
查看>>
熟用TableView
查看>>
Java大数——a^b + b^a
查看>>
poj 3164 最小树形图(朱刘算法)
查看>>
服务器内存泄露 , 重启后恢复问题解决方案
查看>>
android一些细节问题
查看>>
KDESVN中commit时出现containing working copy admin area is missing错误提示
查看>>
利用AOP写2PC框架(二)
查看>>