• Dream City(线性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 nmai and bi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.

    输入

    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)

    输出

    For each test case, output the result in a single line.

    样例输入

    2
    2 1
    10 10
    1 1
    2 2
    8 10
    2 3

    样例输出

    10
    21

    提示

    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,m分别代表n个数,和能砍m棵树,接下来n个数字ai代表第i棵树原有的金币,再接下来n个数字bi代表第i棵树每天增长的硬币。
    将树按b升序排序,然后循环更新dp值即可。
    #include <bits/stdc++.h>
    using namespace std;
    struct p
    {
        int a,b;
    }x[255];
    int dp[255];
    bool cmp(p a,p b)
    {
        if(a.b!=b.b) return a.b<b.b;
        return a.a<b.a;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        int T;
        cin>>T;
        while(T--)
        {
            memset(dp,0,sizeof dp);
            int n,m;
            cin>>n>>m;
            for(int i=0;i<n;i++)
                cin>>x[i].a;
            for(int i=0;i<n;i++)
                cin>>x[i].b;
            sort(x,x+n,cmp);
            for(int i=0;i<n;i++)
                for(int j=m;j>0;j--)
                    dp[j]=max(dp[j],dp[j-1]+x[i].a+x[i].b*(j-1));
            cout<<dp[m]<<'
    ';
        }
        return 0;
    }
  • 相关阅读:
    【机器学习】:Xgboost和GBDT的不同与比较
    golang pprof
    终于解决了AMD RYZEN 3970X的散热问题
    2022,你好
    二叉树的遍历 → 不用递归,还能遍历吗
    异或运算的巧用 → 不用额外的变量,如何交换两个变量的值?
    关于 RocketMQ 事务消息的正确打开方式 → 你学废了吗
    单向链表的花式玩法 → 还在玩反转?
    二叉树的简单实战 → 一起温故下二叉树的遍历
    序列化和反序列化
  • 原文地址:https://www.cnblogs.com/zdragon1104/p/9189171.html
Copyright © 2020-2023  润新知