牛客 Stock Market

题目描述

Despite their innate prudence, the cows took a beating in the home mortgage market and now are trying their hand at stocks. Happily, Bessie is prescient and knows not only today’s S (2 <= S <= 50) stock prices but also the future stock prices for a total of D days (2 <= D <= 10).
Given the matrix of current and future stock prices on various days (1 <= PR_{sd}PR sd<= 1,000) and an initial M (1 <= M <= 200,000) units of money, determine an optimal buying and selling strategy in order to maximize the gain realized by selling stock on the final day. Shares must be purchased in integer multiples, and you need not spend all the money (or any money). It is guaranteed that you will not be able to earn a profit of more than 500,000 units of money.
Consider the example below of a bull (i.e., improving) market, the kind Bessie likes most. In this case, S=2 stocks and D=3 days. The cows have 10 units of money to invest.
Today’s price
| Tomorrow’s price
| | Two days hence
Stock | | |
1 10 15 15
2 13 11 20

If money is to be made, the cows must purchase stock 1 on day 1. Selling stock 1 on day 2 and quickly buying stock 2 yields 4 money in the bank and one share of 2. Selling stock 2 on the final day brings in 20 money for a total of 24 money when the 20 is added to the bank.

输入描述

  • Line 1: Three space-separated integers: S, D, and M
  • Lines 2…S+1: Line s+1 contains the D prices for stock s on days 1…D: PR_{sd}PR sd

输出描述

  • Line 1: The maximum amount of money possible to have after selling on day D.、

输入样例

2 3 10
10 15 15
13 11 20

输出样例

24

解法:

这是一道动态规划dp,我们只需要计算一天购买哪些股票第二天能获得最大的利润。

#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 1e2 + 10;
int aa[maxn][maxn], dp[1000000];
int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> aa[i][j];
        }
    }
    int ans = k;
    for(int i = 1; i < m; i++){ // 最后一天不用在进行计算
        memset(dp, 0, sizeof(dp)); //在第i天  花费多少钱可以获得的最大利润
        for(int j = 1; j <= n; j++){  // 表示第j个商品
            int v = aa[j][i + 1] - aa[j][i]; // 可以获得的利润
            for(int l = aa[j][i]; l <= ans; l++){  // 花费l元的最大利润
                dp[l] = max(dp[l], dp[l - aa[j][i]] + v);
            }
        }
        ans += dp[ans]; // 更新所拥有的的钱
    }
    cout << ans << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值