【暑假】[深入动态规划]UVa 1412 Fund Management
UVa 1412 Fund Management
题目:
UVA - 1412
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<tex2html_verbatim_mark> US Dollars (USD) from individual investors to manage them for m<tex2html_verbatim_mark> days. Management is performed on a day by day basis. Frank has selected n<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> USD per share and the lot size of the corresponding stock is si<tex2html_verbatim_mark> , then Frank can spend pisi<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> later, then Frank can sell this lot for p'isi<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> that can be held by the fund at any given day. There is also the overall limit k<tex2html_verbatim_mark> 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. InputInput consists on several datasets. The first line of each dataset contains four numbers -- c<tex2html_verbatim_mark> , m<tex2html_verbatim_mark> , n<tex2html_verbatim_mark> , and k<tex2html_verbatim_mark> . Here c<tex2html_verbatim_mark>(0.01c100000000.00)<tex2html_verbatim_mark> is the amount of cash collected from individual investors up to a cent (up to two digits after decimal point); m<tex2html_verbatim_mark>(1m100)<tex2html_verbatim_mark> is the number of days in the fund's lifetime; n<tex2html_verbatim_mark>(1n8)<tex2html_verbatim_mark> is the number of stocks selected by Frank for trading;k<tex2html_verbatim_mark>(1k8)<tex2html_verbatim_mark> is the overall limit on the number of lots the fund can hold at any time. The following 2n<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> and ki<tex2html_verbatim_mark> . Here si<tex2html_verbatim_mark>(1si1000000)<tex2html_verbatim_mark> is the lot size of the given stock, and ki<tex2html_verbatim_mark>(1kik)<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> 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. OutputFor each dataset, write to the output file m + 1<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> 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 Input144624.00 9 5 3 Sample Output151205.00 |
思路:
一共有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):
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
using namespace std; const double INF = 1e30;
const int maxn = ;
const int maxm = + ;
const int maxstate = ; int m, n, s[maxn], k[maxn], kk;
double c, price[maxn][maxm];
char name[maxn][]; double d[maxm][maxstate];
int opt[maxm][maxstate], prev[maxm][maxstate]; //配合输出print_ans int buy_next[maxstate][maxn], sell_next[maxstate][maxn];
vector<vector<int> > states;
//states[i]代表一个标号为i的n元组 组信息用vector保存 //一个n元组带包手持各股票的数目
map<vector<int>, int> ID; //ID 是vector到序号的映射 void dfs(int stock, vector<int>& lots, int totlot) { //dfs序构造states
if(stock == n) { //新的n元组构造完成
ID[lots] = states.size(); //ID
states.push_back(lots); //push
}
else for(int i = ; i <= k[stock] && totlot + i <= kk; i++) { //在满足k[]与K的限制下如果可行则dfs下一stock
lots[stock] = i;
dfs(stock+, lots, totlot + i); //回溯 写法
}
} void init() { //利用states离线建立状态之间的关系
vector<int> lots(n);
states.clear(); //clear1
ID.clear(); //clear2
dfs(, lots, ); //return states
for(int s = ; s < states.size(); s++) { //操作一个状态
int totlot = ;
for(int i = ; i < n; i++) totlot += states[s][i]; //目前状态的所有股数
for(int i = ; i < n; i++) { //枚举在状态中改变的股票i
buy_next[s][i] = sell_next[s][i] = -; //初值-1
if(states[s][i] < k[i] && totlot < kk) { //如果buy可行
vector<int> newstate = states[s];
newstate[i]++;
buy_next[s][i] = ID[newstate];
}
if(states[s][i] > ) { //如果sell可行
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) { //刷表法 更新
//在第day天 在进行操作后 状况s转移到状况s2 转移后手中钱数为v
//对|o|进行操作 //opt的正负用以区分操作 buy || sell
if(v > d[day+][s2]) {
d[day+][s2] = v;
opt[day+][s2] = o; //: 得出 [][] 的最优操作
prev[day+][s2] = s; //: 得出 [][] 的最优前状况
}
} double dp() {
for(int day = ; day <= m; day++)
for(int s = ; s < states.size(); s++) d[day][s] = -INF //边界设定 d[][] = c; //第0天手持0手股票 手中有c的钱数
for(int day = ; day < m; day++) //枚举天数
for(int s = ; s < states.size(); s++) { //枚举手中股票的状态
double v = d[day][s];
if(v < -) continue; //return update(day, s, s, v, ); // HOLD
for(int i = ; i < n; i++) {
if(buy_next[s][i] >= && v >= price[i][day] - 1e-) //s状态下要买股票i
update(day, s, buy_next[s][i], v - price[i][day], i+); // BUY
if(sell_next[s][i] >= ) //s状态下要卖股票i
update(day, s, sell_next[s][i], v + price[i][day], -i-); // SELL
}
}
return d[m][]; //到了第m天 手中没有股票 //反对DP原问题的最大值
} void print_ans(int day, int s) { //根据prev与opt递归输出解
if(day == ) return;
print_ans(day-, prev[day][s]);
if(opt[day][s] == ) printf("HOLD\n"); //==0
else if(opt[day][s] > ) printf("BUY %s\n", name[opt[day][s]-]); // >0
else printf("SELL %s\n", name[-opt[day][s]-]); //<0
} int main() {
int kase = ;
while(scanf("%lf%d%d%d", &c, &m, &n, &kk) == ) {
if(kase++ > ) printf("\n"); for(int i = ; i < n; i++) {
scanf("%s%d%d", name[i], &s[i], &k[i]);
for(int j = ; j < m; j++) { scanf("%lf", &price[i][j]); price[i][j] *= s[i]; }
}
init(); double ans = dp();
printf("%.2lf\n", ans);
print_ans(m, );
}
return ;
}
【暑假】[深入动态规划]UVa 1412 Fund Management的更多相关文章
- UVa 1412 Fund Management (预处理+状压DP)
题意:题意很难说清楚自己看原文,链接:UVa 1412 Fund Management 析:总体来说如果没有超时的话,这个题不是特别难,但是这个题很容易超时,主要是体现在状态转移时,很容易想到状态方程 ...
- UVa 1412 - Fund Management(状压DP + 预处理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- uva1412 Fund Management
状压dp 要再看看 例题9-17 /* // UVa1412 Fund Management // 本程序会超时,只是用来示范用编码/解码的方法编写复杂状态动态规划的方法 // Rujia Liu ...
- 【暑假】[深入动态规划]UVa 1628 Pizza Delivery
UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路: ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- 【暑假】[深入动态规划]UVa 12170 Easy Climb
UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路: 引别人一 ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- 【暑假】[深入动态规划]UVa 10618 Fun Game
UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路: 一圈人围坐 ...
随机推荐
- vncserver 添加用户
1.在vncserver设置登录用户的信息 #vi /etc/sysconfig/vncservers VNCSERVERS="1:root 2:wt" 此处添加用户 ...
- winform保存登录cookie
在web程序中,我们通常使会使用cookie来保存一些用户状态,或者权限或者你想保存的东西,但是在CS程序中,如果要使用cookie就必须要做些功课了... 最好注意以下几点: 1.使用成员 ...
- 深入js的面向对象学习篇(封装是一门技术和艺术)——温故知新(二)
下面全面介绍封装和信息隐藏. 通过将一个方法或属性声明为私用的,可以让对象的实现细节对其它对象保密以降低对象之间的耦合程度,可以保持数据的完整性并对其修改方式加以约束.在代码有许多人参与设计的情况下, ...
- [转载]jQuery 1.9 移除了 $.browser 的替代方法获取浏览器类型
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更新的 2.0 版本中,将不再支持 IE 6/7/8. ...
- PAT-乙级-1034. 有理数四则运算(20)
1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求编写程序,计算2个有理 ...
- [Firefly引擎][学习笔记三][已完结]所需模块封装
原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读: 笔记三主要就是各个模块的封装了,这里贴 ...
- hdu 1760 A New Tetris Game 博弈论
找sg值,可以选择暴力,也可以利用sg值的特点简化. 暴力就跟取石子一样,没什么差别,DFS搞定.把矩阵看成一个字符串,字符串就是一个状态. 其实我们也可以不暴力求sg值,因为只要当前状态能到达一个s ...
- jmeter summariser(命令行执行时的输出) 、查看结果树等结果中文乱码
在使用jmeter测试时,如果你的sampler名字为中文.或者输出的结果信息有中文,你会发现它们都是乱码,非常蛋碎!原因是: jmeter的默认编码为:ISO-8859-1, 解决方案就是要修改它 ...
- Eclipse中查看Android模拟器SD卡目录
· 有时候用到Android模拟器来模拟SD卡相关操作,在Eclipse中可以直接查看SD卡目录: 首先,新建模拟器的时候要创建SD卡,存储的大小根据需要创建: 启动模拟器,在Eclipse中打开视图 ...
- 113. Path Sum II
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...