题目:
UVA - 1412
Fund Management
Description Frank is a portfolio manager of a closed-end fund for Advanced Commercial Markets (ACM ). Fund collects money (cash) from individual investors for a certain period of time and invests cash into various securities in accordance with fund's investment strategy. At the end of the period all assets are sold out and cash is distributed among individual investors of the fund proportionally to their share of original investment. Frank manages equity fund that invests money into stock market. His strategy is explained below. Frank's fund has collected c When price of the stock changes to p'i Each stock has its own volatility and risks, so to minimize the overall risk of the fund, for each stock there is the maximum number of lots ki Any trade to buy or sell one lot of stock completely occupies Frank's day, and thus he can perform at most one such trade per day. Frank is not allowed to buy partial lots if there is not enough cash in the fund for a whole lot at the time of purchase. Now, when fund's period has ended, Frank wants to know what is the maximum profit he could have made with this strategy having known the prices of each stock in advance. Your task is to write a program to find it out. It is assumed that there is a single price for each stock for each day that Frank could have bought or sold shares of the stock at. Any overheads such as fees and commissions are ignored, and thus cash spent to buy or gained on a sell of one lot of stock is exactly equal to its price on this day multiplied by the number of shares in a lot.
InputInput consists on several datasets. The first line of each dataset contains four numbers -- c The following 2n The first line for each stock contains the stock name followed by two integer numbers si The second line for each stock contains m Cash and prices in the input file are formatted as a string of decimal digits, optionally followed by a dot with one or two digits after a dot.
OutputFor each dataset, write to the output file m + 1 On the first line write a single decimal number -- the precise value for the maximal amount of cash that can be collected in the fund by the end of its period. The answer will not exceed 1 000 000 000.00. Cash must be formatted as a string of decimal digits, optionally followed by a dot with one or two digits after a dot. On the following m
Sample Input
144624.00 9 5 3 IBM 500 3 97.27 98.31 97.42 98.9 100.07 98.89 98.65 99.34 100.82 GOOG 100 1 467.59 483.26 487.19 483.58 485.5 489.46 499.72 505 504.28 JAVA 1000 2 5.54 5.69 5.6 5.65 5.73 6 6.14 6.06 6.06 MSFT 250 1 29.86 29.81 29.64 29.93 29.96 29.66 30.7 31.21 31.16 ORCL 300 3 17.51 17.68 17.64 17.86 17.82 17.77 17.39 17.5 17.3
Sample Output
151205.00 BUY GOOG BUY IBM BUY IBM HOLD SELL IBM BUY MSFT SELL MSFT SELL GOOG SELL IBM |
思路:
一共有n天,把天数看作阶段,对于每一天,我们可以选择出手或买进一手股票,在最后一天必须将股票全部出手且求解最大钱数。
可以这样定义d[i][s]:表示第i天手中股票的状态为s时手中的最大钱数,采用刷表法更新d[i+1][s'] ,s'表示s经过出手或买进转移的状态。
问题就变成了如何表示状况s?采用n元组的形式。
但不能将一个n元组表示进d数组,这里的方法是离线dfs出全部状态并分别编号,得出状态与相连的关系buy_next与sell_next。那么d中的状态s就可以用一个整数表示了。
另外输出也有一定的技巧,用到了prev 与 opt 数组,并用正负区别操作。
代码(from Rujia Liu):
1 #include2 #include 3 #include 4 #include