uva1412 Fund Management

状压dp

要再看看  例题9-17

/*
// UVa1412 Fund Management
// 本程序会超时,只是用来示范用编码/解码的方法编写复杂状态动态规划的方法
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;

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

map<int, double> d[maxm];
map<int, int> opt[maxm], prevv[maxm];
int m, n, s[maxn], k[maxn], kk;
double c, price[maxn][maxm];
char name[maxn][10];

int encode(int* portfolio) {
  int h = 0;
  for(int i = 0; i < n; i++) h = h * 9 + portfolio[i];
  return h;
}

int decode(int h, int* portfolio) {
  int totlot = 0;
  for(int i = n-1; i >= 0; i--) {
    portfolio[i] = h % 9;
    totlot += portfolio[i];
    h /= 9;
  }
  return totlot;
}

void update(int oldh, int day, int h, double v, int o) {
  if(d[day].count(h) == 0 || v > d[day][h]) {
    d[day][h] = v;
    opt[day][h] = o;
    prevv[day][h] = oldh;
  }
}

double dp() {
  int portfolio[maxn];
  d[0][0] = c;
  for(int day = 0; day < m; day++)
    for(map<int, double>::iterator it = d[day].begin(); it != d[day].end(); it++) {
      int h = it->first;
      double v = it->second;
      int totlot = decode(h, portfolio);

      update(h, day+1, h, v, 0); // HOLD
      for(int i = 0; i < n; i++) {
        if(portfolio[i] < k[i] && totlot < kk && v >= price[i][day] - 1e-3) {
          portfolio[i]++;
          update(h, day+1, encode(portfolio), v - price[i][day], i+1); // BUY
          portfolio[i]--;
        }
        if(portfolio[i] > 0) {
          portfolio[i]--;
          update(h, day+1, encode(portfolio), v + price[i][day], -i-1); // SELL
          portfolio[i]++;
        }
      }
    }
  return d[m][0];
}

void print_ans(int day, int h) {
  if(day == 0) return;
  print_ans(day-1, prevv[day][h]);
  if(opt[day][h] == 0) printf("HOLD\n");
  else if(opt[day][h] > 0) printf("BUY %s\n", name[opt[day][h]-1]);
  else printf("SELL %s\n", name[-opt[day][h]-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]; }
    }
    for(int i = 0; i <= m; i++) { d[i].clear(); opt[i].clear(); prevv[i].clear(); }

    double ans = dp();
    printf("%.2lf\n", ans);
    print_ans(m, 0);
  }
  return 0;
}
*/

#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], prevv[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;
    prevv[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, prevv[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;
}

原文地址:https://www.cnblogs.com/lqerio/p/9860910.html

时间: 2024-11-25 20:22:19

uva1412 Fund Management的相关文章

【暑假】[深入动态规划]UVa 1412 Fund Management

UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Frank is a portfolio manager of a closed-end fund for Advanced Commercial Markets (ACM ). Fund

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

UVa 1412 - Fund Management(状压DP + 预处理)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4158 题意: 你有c(0.01≤c≤1e8)美元现金,但没有股票.给你m(1≤m≤100)天时间和n(1≤n≤8)支股票供你买卖,要求最后一天结束后不持有任何股票,且剩余的钱最多.买股票不能赊账,只能用现金买.已知每只股票每天的价格(0.01-999.99.单位是美元/股)与参数s

投资银行学词汇

主页 > 专业术语 A A,B shares A,B???br/> ABS(asset-backed securities) 资产支持证券 accounts receivable 应收账款 acquisition 收购 acting in concert 一致行???br/> active market 活跃市场 additional float 扩股 administration structure 治理结构 ADR(American Depository Receipt??? 美国存

[SinGuLaRiTy] 动态规划题目复习

[SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metro 题目描述 特工玛利亚被送到S市执行一个特别危险的任务.她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂. 玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰头.玛利亚知道有一个强大的组织正在追踪她,她知道如果一直呆在一个车站,她会有很大的被抓的风险,躲

2014年全球“高被引科学家”数学类名单

名First Name 姓Last Name 学科(Category) 第一单位(Primary Affliation) 第二单位(Secondary Affliation) Saeid Abbasbandy Mathematics Imam Khomeini International University, Iran   Bashir Ahmad Mathematics King Abdulaziz University, Saudi Arabia   Douglas N Arnold Ma

BADI OVERVIEW

什么是BADI,如何从SAP中查找BADI? Business Add-Ins are a new SAP enhancement technique based on ABAP Objects. They can be inserted into the SAP System to accommodate user requirements too specific to be included in the standard delivery. Since specific industri

6.21外刊打卡3

Hedge funds and artificial intelligence 对冲基金与人工智能 商业银行的风险对冲可以分为自我对冲和市场对冲两种情况: 1.所谓自我对冲是指商业银行利用资产负债表或某些具有收益负相关性质的业务组合本身所具有的对冲特性进行风险对冲. 2.市场对冲是指对于无法通过资产负债表和相关业务调整进行自我对冲的风险(又称残余风险),通过衍生产品市场进行对冲. 假如你在10元价位买了一支股票,这个股票未来有可能涨到15元,也有可能跌到7元.你对于收益的期望倒不是太高,更主要的

7.20 1

Hedge funds and artificial intelligence对冲基金与人工智能Return on AIAI 回报AI-driven hedge funds need human brains, too由 AI 操盘的对冲基金也离不开人脑ARTIFICIAL intelligence (AI) has already changed some activities, including parts of finance likefraud prevention, but not