作者: | 来源:互联网 | 2023-10-11 17:00
This problem consists of three subproblems: for solving subproblem F1 you will receive 8 points, for solving subproblem F2 you will receive 15 points, and for solving subproblem F3 you will receive 10
points.
Manao has developed a model to predict the stock price of a company over the next n days and wants to design a profit-maximizing trading algorithm to make
use of these predictions. Unfortunately, Manao‘s trading account has the following restrictions:
It only allows owning either zero or one shares of stock at a time;
It only allows buying or selling a share of this stock once per day;
It allows a maximum of k buy orders over the next n days;
For the purposes of this problem, we define a trade to a be the act of buying one share of stock on day i,
then holding the stock until some day j?>?i at which point the share is sold. To restate the above constraints, Manao is permitted to make at most k non-overlapping
trades during the course of an n-day trading period for which Manao‘s model has predictions about the stock price.
Even though these restrictions limit the amount of profit Manao can make compared to what would be achievable with an unlimited number of trades or the ability to hold more than one share at a time, Manao still has the potential to make a lot of money because
Manao‘s model perfectly predicts the daily price of the stock. For example, using this model, Manao could wait until the price is low, then buy one share and hold until the price reaches a high value, then sell for a profit, and repeat this process up to k times
until n days have passed.
Nevertheless, Manao is not satisfied by having a merely good trading algorithm, and wants to develop an optimal strategy for trading subject to these constraints. Help Manao achieve this goal by writing a program that will determine when to buy and sell stock
to achieve the greatest possible profit during the n-day trading period subject to the above constraints.
Output
For this problem, the program will only report the amount of the optimal profit, rather than a list of trades that can achieve this profit.
Therefore, the program should print one line containing a single integer, the maximum profit Manao can achieve over the next n days with the constraints
of starting with no shares on the first day of trading, always owning either zero or one shares of stock, and buying at mostk shares over the course of
the n-day trading period.
Note
In the first example, the best trade overall is to buy at a price of 1 on day 9 and sell at a price of 9 on
day 10 and the second best trade overall is to buy at a price of 2 on day 1 and sell at a price of 9 on
day 4. Since these two trades do not overlap, both can be made and the profit is the sum of the profits of the two trades. Thus the trade strategy looks like this:
2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9
buy | | | sell | | | | | buy | sell
The total profit is then (9?-?2)?+?(9?-?1)?=?15.
In the second example, even though Manao is allowed up to 5 trades there are only 4 profitable trades available. Making a fifth trade would cost Manao money so he only makes the following 4:
2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9
buy | sell | buy | sell | | buy | sell | | buy | sell
The total profit is then (7?-?2)?+?(9?-?3)?+?(9?-?7)?+?(9?-?1)?=?21.
题意:题目太长了,都懒得看了,直接看样例就知道是什么意思了……给出n和k,然后给出n个数,从答案可以看出:从中选出k对差值最大的取其之和。而且一对buy和sell中间不能有其他的buy和sell嵌入,这由上面可以看出。而且由上面可以看出:被选出的一对buy和sell中的这两个数,不能用做其他对的数,比如第二个样例中(2,7)是一对,(3,9)是一对,虽然(2,9)的差值也更大,但是其中的2与9在其他对中已经使用过,所以(2,9)不是一对了。意思就是其中所选的数不能重复使用!
思路:所以这样的话,就是用DP来做了。DP[ i ][ j ]表示在 i 状态下到第 j 个数所能达到的最大&#20540;。因为要选 k 对个buy 与 sell,所以状态从 1 到 k ;因为有 n 个数,所以 1 <=j<=n 。
状态转移方程:
先扫描过去,算出它们之间的差&#20540;,并且找出最大的一对的差&#20540;;然后第二对就可以在前一对的基础上找出来了。
所以当前的最大&#20540;DP[ i ] [ j ] 就是前一个数的能达到的最大&#20540;(即DP[ i ] [ j-1 ])与当前的能达到的最大&#20540;(max(前一状态的前一个数的最大&#20540;,当前状态能达到的最大&#20540;(即Max&#43;p [ j ])),两者取大者。
/*即: DP[ i ] [ j ] =max(DP[ i ] [ j-1 ] , max(Max&#43;p[ j ],DP[ i-1 ] [ j-1 ]);*/
故: DP[ i ] [ j ] =max(DP[ i ] [ j-1 ] , max(Max,DP[ i-1 ] [ j-1 ] - p[ j ]) &#43;p[ j ]);
而:Max=max(Max,DP[ i-1 ] [ j-1 ] - p[ j ]) ;
这里,Max这个比较难理解。其实就是前一个状态或者当前状态与前一个数的差&#20540;改变量取最大&#20540;,所以到下一个数的时候才能求出与当前&#20540;差&#20540;最大的加入当前buy和sell。
因为本人也对DP比较晕……所以这题也搞了好久,这个状态又单步调试了,又输出中间量了才理解……确实状态好难想出来啊!!!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include