• 【c++刷题Day1】专题1背包T2


    这是c++刷题的Day1
    在这里插入图片描述🗡题目描述
    💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎
    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.
    💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎💎
    🔑思路

    贪心:
    改变顺序, 增长快的放在后面更划算
    接下来直接背包即可

    💯代码:

    #include 
    using namespace std ;
    
    const int N = 300 ;
    int n , t , f[N] ;
    struct Node {
        int a , b ;
        bool operator < (const Node & A) const {
            if (b == A.b)
                return a > A.a ;
            return b < A.b ;
        }
        int v (int x) {
            return a + b * x ;
        }
    } tr[N] ;
    
    int main () {
        int T ;
        cin >> T ;
        while (T -- ) {
            memset (f , -1 , sizeof (f)) ;
            cin >> n >> t ;
            
            for (int i = 0; i < n; i ++)
                cin >> tr[i].a ;
            for (int i = 0; i < n; i ++)
                cin >> tr[i].b ;
                
            sort (tr , tr + n) ;
            
            f[0] = 0 ;
            for (int i = 0; i < n; i ++)
                for (int j = t - 1; j >= 0; j --)
                    if (f[j] != -1)
                        f[j + 1] = max (f[j + 1] , f[j] + tr[i].v (j)) ;
                
            cout << f[t] << endl ;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    在NestJS应用程序中使用 Unleash 实现功能切换的指南
    头部品牌停业整顿,鲜花电商的中场战事迎来拐点?
    Pygame 游戏开发 图形绘制 & 键鼠事件
    网络安全(黑客)自学
    华为23年9月笔试原题,巨详细题解,附有LeetCode测试链接
    还在用Excel做报表?建议你试试这个数据填报系统_光点科技
    Python教程--len函数
    用DIV+CSS技术设计的水果介绍网站(web前端网页制作课作业)
    Linux- curl命令
    Word控件Spire.Doc 【文本】教程(2) ;在 C#、VB.NET 中从 Word 文档中提取文本
  • 原文地址:https://blog.csdn.net/m0_60519493/article/details/126337432