UVA 1412 - Fund Management(用vector容器模拟状态的状压dp)

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 US Dollars (USD) from individual investors to manage them 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, a lot size was 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 pi USD per share and the lot size of the corresponding stock is si , then Frank can spend pisi USD to buy one lot of the corresponding stock for his fund if the fund has enough cash left, thus decreasing available cash in the fund. This trade is completely performed in one day.

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

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 that can be held by the fund at any given day. There is also the overall limit k on 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 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.

Input 

Input consists on several datasets. The first line of each dataset contains four numbers -- c , m , n , and k . Here c (0.01c100000000.00) is the amount of cash collected from individual investors up to a cent (up to two digits after decimal point); m (1m100) is the number of days in the fund's lifetime; n (1n8) is the number of stocks selected by Frank for trading; k (1k8) 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 two lines per stock.

The first line for each stock contains the stock name followed by two integer numbers si and ki . Here si (1si1000000) is the lot size of the given stock, and ki (1kik) is the number of lots of this stock the fund can hold at any time. Stock name consists of 1 to 5 capital Latin letters from ``A" to ``Z". All stock names in the input file are distinct.

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

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.

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 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 lines write the description of Frank's actions for each day that he should have made in order to realize this profit. Write BUY 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 have been done on that day.

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


思路:总共有8中股票,每种最多在一天中有8手,用状压只能用9进制,比较麻烦,这时候可以用vector容器来表示集合,再用map赋予每个集合对应的标号,即相当于状态压缩了,除了这点外,转移方程也都易想到


#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
using namespace std;

const double INF = 1e30;
const int maxn = 8;
const int maxm = 100 + 5;
const int maxstate = 15000;

int m, n, s[maxn], k[maxn], kk;
double c, price[maxn][maxm];
char name[maxn][10];

double 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] = states.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][day] - 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(用vector容器模拟状态的状压dp))