PTA 7-10 Hand-made Cream(8分)

该博客介绍了一个关于烘焙的手工奶油面包问题,目标是通过动态规划算法找到最佳的面包和奶油组合,以获得最大的总口味得分。题目中给出面包和奶油的口味分数,且说明了负分也可能产生美味的面包。博主通过C++实现了一个动态规划解决方案,并给出了样例输入和输出,以及代码运行结果。
摘要由CSDN通过智能技术生成

Suppose you are a baker planning to bake some hand-made cream breads.

To bake a cream bread, we need to use one slice of bread and one kind of cream. Each hand-made cream bread has a taste score to describe how delicious it is, which is obtained by multiplying the taste score for bread and the taste score for cream. (The taste scores could be negative, howerver, two negative tast scores can still produce delicious cream bread.)

The bread and cream are stored separately.

The constraint is that you need to examine the breads in a given order, and for each piece of bread, you have to decide immediately (without looking at the remaining breads) whether you would like to take it.

After you are finished with breads, you will take the same amount of cream in the same manner. The breads and creams you take will be paired in the same order as you take them.

Given N taste scores for bread and M taste scores for cream, you are supposed to calculate the maximum total taste scores for all hand-made cream bread.

Input Specification

Each input file contains one test case. For each case, the first line contains two integers N and M (1≤N,M≤103 ), which are the numbers of bread and cream, respectively.

The second line gives N taste scores for bread.

The third line gives M taste scores for cream.

The taste scores are integers in [−103,103].

All the numbers in a line are separated by a space.

Output Specification

Print in a line the maximum total taste score.

Sample Input

3 4
-1 10 8
10 8 11 9

Sample Output

188

Hint

The maximum total taste score for the sample case is 10×10+8×11=188.
请添加图片描述

限制条件

限制条件数量
代码长度限制16 KB
时间限制100 ms
内存限制64 MB

C++代码

#include <iostream>

using namespace std;

int dp[1001][1001];

int main()
{
    int n, m;
    cin >> n >> m;

    int bread[1001];
    int cream[1001];

    int a;
    for(int i = 0; i < n; i++)
    {
        cin >> a;
        bread[i + 1] = a;
    }
    for(int i = 0; i < m; i++)
    {
        cin >> a;
        cream[i + 1] = a;
    }

    for(int i = 0; i <= n; i++)
    {
        dp[i][0] = 0;
    }
    for(int i = 0; i <= m; i++)
    {
        dp[0][i] = 0;
    }


    int result = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(dp[i - 1][j - 1] + bread[i] * cream[j] >= dp[i][j - 1] && dp[i - 1][j - 1] + bread[i] * cream[j] >= dp[i - 1][j])
            {
                dp[i][j] = dp[i - 1][j - 1] + bread[i] * cream[j];
            }
            else
            {
                if(dp[i][j - 1] > dp[i - 1][j])
                {
                    dp[i][j] = dp[i][j - 1];
                }
                else
                {
                    dp[i][j] = dp[i - 1][j];
                }
            }

            if(dp[i][j] > result)
            {
                result = dp[i][j];
            }
        }
    }
    cout << result;
    return 0;
}

运行结果

请添加图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值