CF:391F1 - Stock Trading 动规DP

来源:互联网 发布:19岁网络女主播萱萱 编辑:程序博客网 时间:2024/06/14 11:23

F1. Stock Trading
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers n and k, separated by a single space, with . The i-th of the following n lines contains a single integer pi (0 ≤ pi ≤ 1012), where pi represents the price at which someone can either buy or sell one share of stock on day i.

The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.

  • In subproblem F1 (8 points), n will be between 1 and 3000, inclusive.
  • In subproblem F2 (15 points), n will be between 1 and 100000, inclusive.
  • In subproblem F3 (10 points), n will be between 1 and 4000000, inclusive.
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.

Sample test(s)
input
10 22739879719
output
15
input
10 52739879719
output
21
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    | 9buy  |      |      | 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    | 9buy  | 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 个数所能达到的最大值。因为要选 k 对个buy 与 sell,所以状态从 1 到 k ;因为有 n 个数,所以 1 <=j<=n 。

状态转移方程:

先扫描过去,算出它们之间的差值,并且找出最大的一对的差值;然后第二对就可以在前一对的基础上找出来了。

所以当前的最大值DP[ i ] [ j ] 就是前一个数的能达到的最大值(即DP[ i ] [ j-1 ])与当前的能达到的最大值(max(前一状态的前一个数的最大值,当前状态能达到的最大值(即Max+p [ j ])),两者取大者。

/*即: DP[ i ] [ j ] =max(DP[ i ] [ j-1 ] , max(Max+p[ j ],DP[ i-1 ] [ j-1 ]);*/

故: DP[ i ] [ j ] =max(DP[ i ] [ j-1 ] , max(Max,DP[ i-1 ] [ j-1 ] -  p[ j ]) +p[ j ]);

而:Max=max(Max,DP[ i-1 ] [ j-1 ] -  p[ j ]) ;

这里,Max这个比较难理解。其实就是前一个状态或者当前状态与前一个数的差值改变量取最大值,所以到下一个数的时候才能求出与当前值差值最大的加入当前buy和sell。

因为本人也对DP比较晕……所以这题也搞了好久,这个状态又单步调试了,又输出中间量了才理解……确实状态好难想出来啊!!!

#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <list>#include <queue>#include <string>#include <cstring>#include <map>#include <stack>#include <set>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define sca(a) scanf("%d",&a)#define pri(a) printf("%d\n",a)#define f(i,a,n) for(i=a;i<n;i++)#define F(i,a,n) for(i=a;i<=n;i++)#define MM 10002#define MN 3005#define INF 16843009000000using namespace std;typedef long long ll;ll dp[MN][MN],p[MN];int main(){    int n,k,i,j;    cin>>n>>k;    F(i,1,n) cin>>p[i];    F(i,1,k)    {        ll M = -INF;        F(j,1,n)        {            M= max(M, dp[i-1][j-1] - p[j]);            dp[i][j] = max(dp[i][j-1], M + p[j]);            //cout << i << " " << j << " " << dp[i][j]<<" mx="<<mx<< endl;        }    }    cout<<dp[k][n]<<endl;    return 0;}



0 0
原创粉丝点击