欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Stock Market(luogu 2938)

发布时间:2025/7/14 编程问答 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Stock Market(luogu 2938) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目描述

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 <= 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.

有S种股票,我们已经知道每一天每一种股票的价格。一共有d天,一开始拥有的钱为m,求最后总共能够拥有多少钱。

输入输出格式

输入格式:

 

* 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

 

输出格式:

 

* 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

Measure:

我们来好好推下这个DP--

  

在第二天,我们能取得的最大收益:

第一种股票第一天10元买入,第二天15元卖出,收益5元

第二种股票第一天……买不起!只有10元

所以,第二天我们就有15元啦。(开心)

在第三天,我们能取得的最大收益:

第一种股票第二天再买没钱赚了

第二种股票第二天则可以低价买入,高价卖出,收益9元

 

其实我一开始有些疑惑,我本来以为最便宜的时候买入,在最贵的时候卖出才应该最优

而DP的思路则是有钱赚就卖

现在验证,它是对的。

假如我们一开始有 m=10 元,一种股票三天价格如下

5 10 20

按我的想法:第一天花10元买入两股,第三天20元卖出,收益:40-10=30

按DP的做法:第一天花10元买入两股,第二天10元卖出,收益:20-10=10,m=20;第三天有钱赚,又第二天卖出两股【就相当于没卖】,收益:+(40-20)= 30

并不影响

 

code(开 O(2) 才过)

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MX=510000; int n,d,m,f[MX],pri[110][20];int main() {scanf("%d%d%d",&n,&d,&m);for(int i=1;i<=n;++i) for(int j=1;j<=d;++j) scanf("%d",&pri[i][j]); for(int k=2;k<=d;++k) { //前 k 天 int mx=0;memset(f,0,sizeof(f));for(int i=1;i<=n;++i) { //第 i 种股票 for(int j=pri[i][k-1];j<=m;++j) { //花 j 元 f[j]=max(f[j],f[j-pri[i][k-1]]+pri[i][k]-pri[i][k-1]); mx=max(mx,f[j]);}}m+=mx; }printf("%d",m);return 0; }

 

转载于:https://www.cnblogs.com/qseer/p/9777188.html

总结

以上是生活随笔为你收集整理的Stock Market(luogu 2938)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。