状压dp

要再看看  例题9-17

  1. /*
  2. // UVa1412 Fund Management
  3. // 本程序会超时,只是用来示范用编码/解码的方法编写复杂状态动态规划的方法
  4. // Rujia Liu
  5. #include<cstdio>
  6. #include<cstring>
  7. #include<map>
  8. using namespace std;
  9.  
  10. const double INF = 1e30;
  11. const int maxn = 8;
  12. const int maxm = 100 + 5;
  13.  
  14. map<int, double> d[maxm];
  15. map<int, int> opt[maxm], prevv[maxm];
  16. int m, n, s[maxn], k[maxn], kk;
  17. double c, price[maxn][maxm];
  18. char name[maxn][10];
  19.  
  20. int encode(int* portfolio) {
  21. int h = 0;
  22. for(int i = 0; i < n; i++) h = h * 9 + portfolio[i];
  23. return h;
  24. }
  25.  
  26. int decode(int h, int* portfolio) {
  27. int totlot = 0;
  28. for(int i = n-1; i >= 0; i--) {
  29. portfolio[i] = h % 9;
  30. totlot += portfolio[i];
  31. h /= 9;
  32. }
  33. return totlot;
  34. }
  35.  
  36. void update(int oldh, int day, int h, double v, int o) {
  37. if(d[day].count(h) == 0 || v > d[day][h]) {
  38. d[day][h] = v;
  39. opt[day][h] = o;
  40. prevv[day][h] = oldh;
  41. }
  42. }
  43.  
  44. double dp() {
  45. int portfolio[maxn];
  46. d[0][0] = c;
  47. for(int day = 0; day < m; day++)
  48. for(map<int, double>::iterator it = d[day].begin(); it != d[day].end(); it++) {
  49. int h = it->first;
  50. double v = it->second;
  51. int totlot = decode(h, portfolio);
  52.  
  53. update(h, day+1, h, v, 0); // HOLD
  54. for(int i = 0; i < n; i++) {
  55. if(portfolio[i] < k[i] && totlot < kk && v >= price[i][day] - 1e-3) {
  56. portfolio[i]++;
  57. update(h, day+1, encode(portfolio), v - price[i][day], i+1); // BUY
  58. portfolio[i]--;
  59. }
  60. if(portfolio[i] > 0) {
  61. portfolio[i]--;
  62. update(h, day+1, encode(portfolio), v + price[i][day], -i-1); // SELL
  63. portfolio[i]++;
  64. }
  65. }
  66. }
  67. return d[m][0];
  68. }
  69.  
  70. void print_ans(int day, int h) {
  71. if(day == 0) return;
  72. print_ans(day-1, prevv[day][h]);
  73. if(opt[day][h] == 0) printf("HOLD\n");
  74. else if(opt[day][h] > 0) printf("BUY %s\n", name[opt[day][h]-1]);
  75. else printf("SELL %s\n", name[-opt[day][h]-1]);
  76. }
  77.  
  78. int main() {
  79. int kase = 0;
  80. while(scanf("%lf%d%d%d", &c, &m, &n, &kk) == 4) {
  81. if(kase++ > 0) printf("\n");
  82. for(int i = 0; i < n; i++) {
  83. scanf("%s%d%d", name[i], &s[i], &k[i]);
  84. for(int j = 0; j < m; j++) { scanf("%lf", &price[i][j]); price[i][j] *= s[i]; }
  85. }
  86. for(int i = 0; i <= m; i++) { d[i].clear(); opt[i].clear(); prevv[i].clear(); }
  87.  
  88. double ans = dp();
  89. printf("%.2lf\n", ans);
  90. print_ans(m, 0);
  91. }
  92. return 0;
  93. }
  94. */
  95.  
  96. #include<cstdio>
  97. #include<cstring>
  98. #include<vector>
  99. #include<map>
  100. using namespace std;
  101.  
  102. const double INF = 1e30;
  103. const int maxn = ;
  104. const int maxm = + ;
  105. const int maxstate = ;
  106.  
  107. int m, n, s[maxn], k[maxn], kk;
  108. double c, price[maxn][maxm];
  109. char name[maxn][];
  110.  
  111. double d[maxm][maxstate];
  112. int opt[maxm][maxstate], prevv[maxm][maxstate];
  113.  
  114. int buy_next[maxstate][maxn], sell_next[maxstate][maxn];
  115. vector<vector<int> > states;
  116. map<vector<int>, int> ID;
  117.  
  118. void dfs(int stock, vector<int>& lots, int totlot) {
  119. if(stock == n) {
  120. ID[lots] = states.size();
  121. states.push_back(lots);
  122. }
  123. else for(int i = ; i <= k[stock] && totlot + i <= kk; i++) {
  124. lots[stock] = i;
  125. dfs(stock+, lots, totlot + i);
  126. }
  127. }
  128.  
  129. void init() {
  130. vector<int> lots(n);
  131. states.clear();
  132. ID.clear();
  133. dfs(, lots, );
  134. for(int s = ; s < states.size(); s++) {
  135. int totlot = ;
  136. for(int i = ; i < n; i++) totlot += states[s][i];
  137. for(int i = ; i < n; i++) {
  138. buy_next[s][i] = sell_next[s][i] = -;
  139. if(states[s][i] < k[i] && totlot < kk) {
  140. vector<int> newstate = states[s];
  141. newstate[i]++;
  142. buy_next[s][i] = ID[newstate];
  143. }
  144. if(states[s][i] > ) {
  145. vector<int> newstate = states[s];
  146. newstate[i]--;
  147. sell_next[s][i] = ID[newstate];
  148. }
  149. }
  150. }
  151. }
  152.  
  153. void update(int day, int s, int s2, double v, int o) {
  154. if(v > d[day+][s2]) {
  155. d[day+][s2] = v;
  156. opt[day+][s2] = o;
  157. prevv[day+][s2] = s;
  158. }
  159. }
  160.  
  161. double dp() {
  162. for(int day = ; day <= m; day++)
  163. for(int s = ; s < states.size(); s++) d[day][s] = -INF;
  164.  
  165. d[][] = c;
  166. for(int day = ; day < m; day++)
  167. for(int s = ; s < states.size(); s++) {
  168. double v = d[day][s];
  169. if(v < -) continue;
  170.  
  171. update(day, s, s, v, ); // HOLD
  172. for(int i = ; i < n; i++) {
  173. if(buy_next[s][i] >= && v >= price[i][day] - 1e-)
  174. update(day, s, buy_next[s][i], v - price[i][day], i+); // BUY
  175. if(sell_next[s][i] >= )
  176. update(day, s, sell_next[s][i], v + price[i][day], -i-); // SELL
  177. }
  178. }
  179. return d[m][];
  180. }
  181.  
  182. void print_ans(int day, int s) {
  183. if(day == ) return;
  184. print_ans(day-, prevv[day][s]);
  185. if(opt[day][s] == ) printf("HOLD\n");
  186. else if(opt[day][s] > ) printf("BUY %s\n", name[opt[day][s]-]);
  187. else printf("SELL %s\n", name[-opt[day][s]-]);
  188. }
  189.  
  190. int main() {
  191. int kase = ;
  192. while(scanf("%lf%d%d%d", &c, &m, &n, &kk) == ) {
  193. if(kase++ > ) printf("\n");
  194.  
  195. for(int i = ; i < n; i++) {
  196. scanf("%s%d%d", name[i], &s[i], &k[i]);
  197. for(int j = ; j < m; j++) { scanf("%lf", &price[i][j]); price[i][j] *= s[i]; }
  198. }
  199. init();
  200.  
  201. double ans = dp();
  202. printf("%.2lf\n", ans);
  203. print_ans(m, );
  204. }
  205. return ;
  206. }

uva1412 Fund Management的更多相关文章

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

    UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS   Memory Limit: Unknown   ...

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

    题意:题意很难说清楚自己看原文,链接:UVa 1412 Fund Management 析:总体来说如果没有超时的话,这个题不是特别难,但是这个题很容易超时,主要是体现在状态转移时,很容易想到状态方程 ...

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

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  5. POJ3570 Fund Management 动态规划

    题目大意 Frank从个人投资者获得了c美元的资金,可用于m天的投资.Frank可以对n(n<=8)支股票进行投资.对于每一支股票:都有一个交易上限si,表示一天最多能交易的股数:还有一个上限k ...

  6. 近期做的一些DP

    UVa 1625 color length https://blog.csdn.net/Dylan_Frank/article/details/52261424 https://www.cnblogs ...

  7. 7.25 9figting!

    TEXT 87 Fund management基金管理   A Miller's tale 米勒传奇(陈继龙编译) Dec 7th 2006 From The Economist print edit ...

  8. 7.25 8figting!

    TEXT 87 Fund management基金管理   A Miller's tale 米勒传奇(陈继龙编译) Dec 7th 2006 From The Economist print edit ...

  9. DP 题集 2

    关于 DP 的一些题目 String painter 先区间 DP,\(dp[l][r]\) 表示把一个空串涂成 \(t[l,r]\) 这个子串的最小花费.再考虑 \(s\) 字符串,\(f[i]\) ...

随机推荐

  1. 洛谷P2473奖励关——状压DP

    题目:https://www.luogu.org/problemnew/show/P2473 还是对DP套路不熟悉... 像这种前面影响后面,而后面不影响前面的问题就应该考虑倒序递推: 看n只有15那 ...

  2. .NETFramework:HttpContext

    ylbtech-.NETFramework:HttpContext 1.返回顶部 1. #region 程序集 System.Web, Version=4.0.0.0, Culture=neutral ...

  3. 超实用的JavaScript技巧及最佳实践给

    1.数组创建一个随机项 var items = [12,548,'a',2,5478,'foo',8852,,'Doe',2145,119]; var randomItem = items[Math. ...

  4. ORACLE PL/SQL 实例精解之第三章 PL/SQL中的SQL

    3.1 在PL/SQL中使用DML 在PL/SQL语块中,两种变量赋值a. := 初始化.b. select into语法,PL/SQL语块的声明部分被声明的变量,后期可以使用选择语句进行赋值. 3. ...

  5. SCUT - 290 - PARCO的因数游戏 - 博弈论

    https://scut.online/p/290 一个 N 个数的取数游戏,Kaildls 和 Parco 轮流操作,每次操作从 N 个数中取一个数 y 并把他变成 y-x(满足 x | y 且x  ...

  6. Navicat Premium连接服务器数据库

    解决Navicat 连接服务器失败的问题 由于服务器的安全问题,有些东西默认是关闭的.就像远程连接服务器的数据库一样,如果默认是每个IP都能访问,安全性就会大大降低,甚至没有安全性可言.但是由于项目需 ...

  7. string类的常用的几个小东西find,substr

    头文件: #include<iostream> #include<string> 定义: string ss; #include<iostream> #includ ...

  8. Hello world——程序员的第一篇代码

    Hello world 是和A+B问题并驾齐驱的一道题,也是当世的经典题之一. 题目: 输出“Hello world”    样例输入 样例输出    Hello world Hello world ...

  9. (十)SpringBoot的文件上传

    一:添加commons-fileupload依赖 打开pom文件添加 <dependency> <groupId>commons-fileupload</groupId& ...

  10. spring cloud feign 调用接口报错"No message available

    There was an unexpected error (type=Internal Server Error, status=500). status 404 reading HelloServ ...