UVA 1412-fund Management (analogue pressure DP with vector container)

Source: Internet
Author: User
Tags integer numbers stock prices

Frank is a portfolio manager of a closed-end fund for advanced Commercial (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 be sold out and cash is distributed among individual investors of the fund proportion Ally to their share of original investment.

Frank manages equity fund that invests money into the stock market. His strategy is explained below.

Frank's fund have collected c US Dollars (USD) from individual investors to manage th EM for m days. Management is performed on a day by day basis. Frank has selected n stocks to invest into. Depending on the overall price range and availability of each stock, Alot size is chosen for Each stock – The number of shares of the stock Frank can buy or sell per day without affecting the market too much by his Trades. So, if the price of the stock Is p i USD per share and the lot size of the corresponding Stock Is s i , then Frank can spend P I s i USD to buy one lot of the corresponding stock for his fund if the Fund have enough cash left, thus decreasing available CAs h in the fund. This trade is completely performed on one day.

When price of the stock changes to p 'i later and then Frank can sell this lot forP 'is I USD, thus increasing available cash for further trading. This trade is also completely performed on one day. All lots of stocks that is held by the fund must is sold by the end of the fund ' s period, so that at the end of the Beginning) The fund is holding only cash.

Each stock have its own volatility and risks, so-minimize the overall risk of the fund, for each stock there are the maxi Mum number of lotski that can is held by the Fund on any given day. There is also the overall limitK in the number of lots of all stocks that the fund can hold at any given day.

Any trade to buy or sell one lot of stock completely occupies Frank's day, and thus he can perform at the most one such trade Per day. Frank is isn't allowed to buy partial lots if there are not enough cash in the fund for a whole IoT at the time of purchase.

Now, when fund's period has ended, Frank wants to know what's the maximum profit he could had made with this strategy ha Ving Tsun Known the prices of each stock in advance. Your task is-to-write a program-to-find it out.

It is assumed, there is a, single, price, each, and the could has bought or sold shares of the Stock at. Any overheads such as fees and commissions is ignored, and thus cash spent to buy or gained on a sell the one lot of stock is exactly equal to it price on this day multiplied by the number of shares in a lot.

Input

Input consists on several datasets. The first line of each dataset contains four numbers-- c , m , n , and K . Here c (0.01 c 100000000.00) is the amount of Cash collected from individual investors up to a cent (up to both digits after decimal point); m (1 m ) is the number of days in the fund ' s Lifetime n (1 n 8) is the number of stocks selected by Frank for trading; K (1 k 8) is the overall limit on the number of lots the fund can hold at any time.

The following 2n Lines describe stocks and their prices with the lines per stock.

The first line for each of the stock contains the stock name followed by both integer numberssi and ki . Heresi (1si1000000) are the lot size of the given stock, and ki c12> (1kiK) is the number of lots in this stock, the fund can hold at any time. Stock name consists of 1 to 5 capital Latin letters from 'A' to 'Z'. All of the stock names in the input file is distinct.

The second line for each stock contains m decimal numbers separated by spaces that denote prices of the Correspon Ding stock for each day in the fund ' s lifetime. The Stock prices is in range from 0.01 to 999.99 (inclusive) given up to a cent (up to both digits after decimal point).

Cash and prices in the input file is formatted as a string of decimal digits, optionally followed by a dot with one or TW o digits after a dot.

Output

For each dataset, write to the output file m + 1 lines. Print a blank line between datasets.

On the first line write a single decimal number--the precise value for the maximal amount of the cash so can be collected The fund by the end of its period. The answer would not exceed 1 000 000 000.00. Cash must be formatted as a string of the decimal digits, optionally followed by a dot with one or both digits after a dot.

On the following m lines write the description of Frank's actions for each day that he should has made in order To realize this profit. WriteBUY followed by a space and a stock name for buying a stock. Write SELL followed by a space and a stock name for selling a stock. Write Hold If nothing should has been done on the that day.

Sample Input

144624.00 9 5 3 IBM 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 48  5.5 489.46 499.72 505 504.28 JAVA 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


Ideas: A total of 8 in the stock, each of the most in a day there are 8 hands, with the pressure can only use 9 of the system, more trouble, this time you can use a vector container to represent the set, and then map to each set corresponding to the label, that is equivalent to state compression, in addition to this, the transfer equation is easy to think


#include <cstdio> #include <cstring> #include <vector> #include <map>using namespace Std;const Double INF = 1e30;const int MAXN = 8;const int MAXM = + 5;const int maxstate = 15000;int m, n, S[maxn], K[MAXN], Kk;do Uble C, Price[maxn][maxm];char name[maxn][10];d ouble d[maxm][maxstate];int opt[maxm][maxstate], prev[maxm][maxstate] ; int BUY_NEXT[MAXSTATE][MAXN], sell_next[maxstate][maxn];vector<vector<int> > states;map<vector< Int>, int> id;void dfs (int stock, vector<int>& lots, int totlot) {if (stock = = N) {Id[lots] = s        Tates.size ();    States.push_back (lots);        } else for (int i = 0; I <= k[stock] && totlot + i <= kk; i++) {Lots[stock] = i;    DFS (stock+1, lots, totlot + i);    }}void init () {vector<int> lots (n);    States.clear ();    Id.clear ();    DFS (0, lots, 0);        for (int s = 0; s < states.size (); s++) {int totlot = 0; for (int i = 0; i < n; i++) Totlot + = StAtes[s][i];            for (int i = 0; i < n; i++) {buy_next[s][i] = sell_next[s][i] =-1;                if (States[s][i] < K[i] && Totlot < KK) {vector<int> newstate = States[s];                newstate[i]++;            Buy_next[s][i] = id[newstate];                } if (States[s][i] > 0) {vector<int> newstate = States[s];                newstate[i]--;            Sell_next[s][i] = id[newstate]; }}}}void Update (int day, int s, int s2, double v, int o) {if (V > d[day+1][s2]) {D[DAY+1][S2] =        V        OPT[DAY+1][S2] = o;    PREV[DAY+1][S2] = s;     }}double DP () {for (int day = 0; day <= m; day++) for (int s = 0; s < states.size (); s++) d[day][s] =-inf;    D[0][0] = c;            for (int day = 0, day < m; day++) for (int s = 0; s < states.size (); s++) {Double v = d[day][s];            if (v <-1) continue; Update (day, S, S,V, 0); Hold for (int i = 0; i < n; i++) {if (Buy_next[s][i] >= 0 && v >= price[i][da Y]-1e-3) Update (day, S, Buy_next[s][i], V-price[i][day], i+1);  BUY if (Sell_next[s][i] >= 0) Update (day, S, Sell_next[s][i], V + price[i][day],-i-1); SELL}} return d[m][0];}    void Print_ans (int day, int s) {if (day = = 0) return;    Print_ans (Day-1, Prev[day][s]);    if (opt[day][s] = = 0) printf ("hold\n");    else if (Opt[day][s] > 0) printf ("BUY%s\n", name[opt[day][s]-1]); else printf ("SELL%s\n", Name[-opt[day][s]-1]);}    int main () {int kase = 0;        while (scanf ("%lf%d%d%d", &c, &m, &n, &kk) = = 4) {if (kase++ > 0) printf ("\ n");            for (int i = 0; i < n; i++) {scanf ("%s%d%d", Name[i], &s[i], &k[i]);        for (int j = 0; J < m; J + +) {scanf ("%lf", &price[i][j]); Price[i][j] *= s[i];}   } init ();     Double ans = dp ();        printf ("%.2lf\n", ans);    Print_ans (m, 0); } return 0;}


UVA 1412-fund Management (analogue pressure DP with vector container)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.