ZOJ-3211-Dream City【6th浙江省赛】【dp】

ZOJ-3211-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 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.

题目链接:ZOJ-3211

题目大意:有一个果园有n棵树,每棵树初始果实a颗,每日新增b颗。某人每日可砍1树,从第一天起连续砍,至多m天。问最多能获得多少果实

题目思路:经典dp,01背包问题。

需要注意:

  1. 斜率小的物品,如果在某个阶段不选,那么在以后都不会再选。因为它增长得慢,如果在开始的时候没选它,那到后面别人长得比它快就肯定轮不到它了。

  2. 可将问题转化为01背包问题。dp[i][j]表示前i课树,第j天所能取到的最大值。

    状态转移方程 : dp[i][j] = max(dp[i-1][j], dp[i-1][j-1] + tree[i].a + (j-1)*tree[i].b) // (tree[i].a为初始值,tree[i].b为每天增加的价值)

参考博客: here

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int dp[300][300]; //前i课树,第j天所能取到的最大值 
struct node
{
    int a,b;
}tree[300];
bool cmp(node x,node y){ return x.b < y.b; };
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        int n,m;
        cin >> n >> m;
        for (int i = 1; i <= n; i++) cin >> tree[i].a;
        for (int i = 1; i <= n; i++) cin >> tree[i].b;
        sort(tree + 1,tree + n + 1,cmp);  //dp是算出取哪几颗树,但是这几棵树的砍得顺序不一定。砍得顺序不同,导致结果不同。所以先处理好顺序。
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= min(i,m); j++)  //看网上很多代码都是算到m其实到min(i,m)就可以。
            {
                dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - 1] + tree[i].a + tree[i].b * (j - 1)); //第i棵树取或者不取 
            }
        }
        cout << dp[n][m] << endl;
    } 
    return 0;
}


你可能感兴趣的:(dp,ZOJ,3211)