Dream City ZOJ - 3211(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 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

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.

题意:有n棵树,初始值为a[i],他们每一天增值b[i],但是每天需要砍掉一棵树,获得它到目前的总价值,问m天的时候能获得的最大价值是多少(必须每天砍一棵树)

思路:想复杂了,原来的代码加了个排序就过了…贪心思想,把增值大的放到后面获得的价值肯定越大
dp[i][j]表示前i棵树在第j天的时候能获得的最大价值。

转移方程是 dp[i][j]=max(dp[i-1][j-1]+a[i]+b[i]*(j-1),dp[i-1][j])

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef pair<int,int> pa;
pa a[300];
int dp[300][300];
bool cmp(pa x,pa y)
{
	if(x.second==y.second)
		return x.first<y.first;
	return x.second<y.second;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m; 
		for(int i=1;i<=n;i++)
			cin>>a[i].first;
		for(int i=1;i<=n;i++)
			cin>>a[i].second;
		sort(a+1,a+n+1,cmp);
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				dp[i][j]=max(dp[i][j],dp[i-1][j-1]+a[i].first+a[i].second*(j-1));
				if(i>j)
				{
					dp[i][j]=max(dp[i][j],dp[i-1][j]);
				}
			}
		cout<<dp[n][m]<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值