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 = ;
- 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], 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 = ; i <= k[stock] && totlot + i <= kk; i++) {
- lots[stock] = i;
- dfs(stock+, lots, totlot + i);
- }
- }
- void init() {
- vector<int> lots(n);
- states.clear();
- ID.clear();
- dfs(, lots, );
- 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++) {
- buy_next[s][i] = sell_next[s][i] = -;
- 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] > ) {
- 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+][s2]) {
- d[day+][s2] = v;
- opt[day+][s2] = o;
- prevv[day+][s2] = s;
- }
- }
- double dp() {
- for(int day = ; day <= m; day++)
- for(int s = ; s < states.size(); s++) d[day][s] = -INF;
- d[][] = c;
- for(int day = ; day < m; day++)
- for(int s = ; s < states.size(); s++) {
- double v = d[day][s];
- if(v < -) continue;
- update(day, s, s, v, ); // HOLD
- for(int i = ; i < n; i++) {
- if(buy_next[s][i] >= && v >= price[i][day] - 1e-)
- update(day, s, buy_next[s][i], v - price[i][day], i+); // BUY
- if(sell_next[s][i] >= )
- update(day, s, sell_next[s][i], v + price[i][day], -i-); // SELL
- }
- }
- return d[m][];
- }
- void print_ans(int day, int s) {
- if(day == ) return;
- print_ans(day-, prevv[day][s]);
- if(opt[day][s] == ) printf("HOLD\n");
- else if(opt[day][s] > ) printf("BUY %s\n", name[opt[day][s]-]);
- else printf("SELL %s\n", name[-opt[day][s]-]);
- }
- 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 ;
- }
uva1412 Fund Management的更多相关文章
- 【暑假】[深入动态规划]UVa 1412 Fund Management
UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS Memory Limit: Unknown ...
- 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 ...
- POJ3570 Fund Management 动态规划
题目大意 Frank从个人投资者获得了c美元的资金,可用于m天的投资.Frank可以对n(n<=8)支股票进行投资.对于每一支股票:都有一个交易上限si,表示一天最多能交易的股数:还有一个上限k ...
- 近期做的一些DP
UVa 1625 color length https://blog.csdn.net/Dylan_Frank/article/details/52261424 https://www.cnblogs ...
- 7.25 9figting!
TEXT 87 Fund management基金管理 A Miller's tale 米勒传奇(陈继龙编译) Dec 7th 2006 From The Economist print edit ...
- 7.25 8figting!
TEXT 87 Fund management基金管理 A Miller's tale 米勒传奇(陈继龙编译) Dec 7th 2006 From The Economist print edit ...
- DP 题集 2
关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...
随机推荐
- 洛谷P2473奖励关——状压DP
题目:https://www.luogu.org/problemnew/show/P2473 还是对DP套路不熟悉... 像这种前面影响后面,而后面不影响前面的问题就应该考虑倒序递推: 看n只有15那 ...
- .NETFramework:HttpContext
ylbtech-.NETFramework:HttpContext 1.返回顶部 1. #region 程序集 System.Web, Version=4.0.0.0, Culture=neutral ...
- 超实用的JavaScript技巧及最佳实践给
1.数组创建一个随机项 var items = [12,548,'a',2,5478,'foo',8852,,'Doe',2145,119]; var randomItem = items[Math. ...
- ORACLE PL/SQL 实例精解之第三章 PL/SQL中的SQL
3.1 在PL/SQL中使用DML 在PL/SQL语块中,两种变量赋值a. := 初始化.b. select into语法,PL/SQL语块的声明部分被声明的变量,后期可以使用选择语句进行赋值. 3. ...
- SCUT - 290 - PARCO的因数游戏 - 博弈论
https://scut.online/p/290 一个 N 个数的取数游戏,Kaildls 和 Parco 轮流操作,每次操作从 N 个数中取一个数 y 并把他变成 y-x(满足 x | y 且x ...
- Navicat Premium连接服务器数据库
解决Navicat 连接服务器失败的问题 由于服务器的安全问题,有些东西默认是关闭的.就像远程连接服务器的数据库一样,如果默认是每个IP都能访问,安全性就会大大降低,甚至没有安全性可言.但是由于项目需 ...
- string类的常用的几个小东西find,substr
头文件: #include<iostream> #include<string> 定义: string ss; #include<iostream> #includ ...
- Hello world——程序员的第一篇代码
Hello world 是和A+B问题并驾齐驱的一道题,也是当世的经典题之一. 题目: 输出“Hello world” 样例输入 样例输出 Hello world Hello world ...
- (十)SpringBoot的文件上传
一:添加commons-fileupload依赖 打开pom文件添加 <dependency> <groupId>commons-fileupload</groupId& ...
- spring cloud feign 调用接口报错"No message available
There was an unexpected error (type=Internal Server Error, status=500). status 404 reading HelloServ ...