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 n, m, ai 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.
题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上每天回结出金子。已经有n棵树,JAVAMAN要停留m天,每天只能砍掉一棵树,砍掉树后就能得到树上的黄金。给定n棵树上原有的黄金a[i]和每天可以新增加的黄金b[i],求他最多可以得到多少黄金。中途如果有1天不砍树的话,之后的日子久不能砍树,所有最好每天都砍树,或者直到树被砍完。
分析:每天长果实多的树在后面砍,先按照b[i]来排序。再写出dp方程:dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+p[i].a+p[i].b*(j-1))
dp[i][j]表示前i课树在j天中最多可得到的黄金
代码:
#include "string.h"
#include "iostream"
#include "algorithm"
using namespace std;
struct dd
{
int a,b;
}p[300];
int cmp(dd p1,dd p2)
{
return p1.b<p2.b;
}
int main()
{
int t,n,m,i,j,dp[300][300];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%d",&p[i].a);
for(i=1;i<=n;i++)
scanf("%d",&p[i].b);
sort(p+1,p+1+n,cmp);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+p[i].a+p[i].b*(j-1));
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}
浙公网安备 33010602011771号