您的位置:首页 > 其它

ZOJ 3211 Dream City

2015-02-25 00:17 375 查看
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; background-color: rgb(255, 255, 255);">Description</span>


J***AMAN 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, J***AMAN 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 J***AMAN 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: J***AMAN just cut tree 1 to get 10 gold coins at the first day.

Test case 2: J***AMAN 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件物品,取的顺序一定是b小的在前。

所以按照b的大小排序,接下来就按照背包的思想做就好了。

#include<iostream>  
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 305;
int T, f[maxn], n, m;
struct abc
{
	int l, r;
}a[maxn];

bool operator <(const abc&a, const abc&b)
{
	if (a.r == b.r) return a.l < b.l;
	return a.r < b.r;
}

int main(){
	cin >> T;
	while (T--)
	{
		cin >> n >> m;	memset(f, 0, sizeof(f));
		for (int i = 0; i < n; i++) scanf("%d", &a[i].l);
		for (int i = 0; i < n; i++) scanf("%d", &a[i].r);
		sort(a , a + n);
		for (int i = 0; i < n;i++)
			for (int j = m; j > 0; j--)
				f[j] = max(f[j], f[j - 1] + a[i].l + a[i].r * (j - 1));
		cout << f[m] << endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: