跟矩阵链乘同类型的题……

输出用%llu不是%I64u……

几组数据:

14
1+2*4+3*4+5*0
0*5*6+7*3+2
3+0+6+7+0+4
4*5+7*1*1+1
2*0+3*4*0+5*6+7+8
1+2+3*1+2*1+0
0+2*2+3*0+4
8*9*0+2
2*0+1+0*3
2*0*3+7+1*0*3
1+3*0*5+2
1+1+1+1+1
2*1+1+2*1+2*1+6
1*2*4*0*6*3

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <cctype>
  5. #include <algorithm>
  6.  
  7. #define LL unsigned long long int
  8.  
  9. using namespace std;
  10.  
  11. const int MAXN = ;
  12.  
  13. int cntNum, cntOp;
  14. char str[MAXN];
  15. char op[MAXN];
  16. LL num[MAXN];
  17. bool vis[MAXN][MAXN];
  18. LL dpMax[MAXN][MAXN];
  19. LL dpMin[MAXN][MAXN];
  20.  
  21. void show()
  22. {
  23. printf("All nums: \n");
  24. for ( int i = ; i < cntNum; ++i )
  25. printf( "%I64d ", num[i] );
  26.  
  27. printf("\nAll Ops:\n");
  28. for ( int i = ; i < cntOp; ++i )
  29. putchar( op[i] );
  30. puts("");
  31. return;
  32. }
  33.  
  34. void chuli()
  35. {
  36. cntNum = cntOp = ;
  37. int len = strlen(str);
  38. for ( int i = ; i < len; ++i )
  39. {
  40. LL tmp = ;
  41. while ( i < len && isdigit( str[i] ) )
  42. {
  43. tmp = tmp * + ( str[i] - '' );
  44. ++i;
  45. }
  46. num[ cntNum++ ] = tmp;
  47. if ( i < len )
  48. op[ cntOp++ ] = str[i];
  49. }
  50.  
  51. //show();
  52. return;
  53. }
  54.  
  55. LL DPMAX( int l, int r )
  56. {
  57. LL &res = dpMax[l][r];
  58. if ( vis[l][r] ) return res;
  59.  
  60. vis[l][r] = true;
  61. if ( l == r ) return res = num[l];
  62. res = ;
  63.  
  64. for ( int k = l; k < r; ++k )
  65. {
  66. if ( op[k] == '+' )
  67. res = max( res, DPMAX( l, k ) + DPMAX( k + , r ) );
  68. else if ( op[k] == '*' )
  69. res = max( res, DPMAX( l, k ) * DPMAX( k + , r ) );
  70. }
  71. //printf( "dp[%d][%d]=%I64u\n", l, r, res );
  72.  
  73. return res;
  74. }
  75.  
  76. LL DPMIN( int l, int r )
  77. {
  78. LL &res = dpMin[l][r];
  79. if ( vis[l][r] ) return res;
  80.  
  81. vis[l][r] = true;
  82. if ( l == r ) return res = num[l];
  83. bool first = true;
  84.  
  85. for ( int k = l; k < r; ++k )
  86. {
  87. if ( op[k] == '+' )
  88. {
  89. if ( first )
  90. {
  91. res = DPMIN( l, k ) + DPMIN( k + , r );
  92. first = false;
  93. }
  94. else
  95. res = min( res, DPMIN( l, k ) + DPMIN( k + , r ) );
  96. }
  97. else if ( op[k] == '*' )
  98. {
  99. if ( first )
  100. {
  101. res = DPMIN( l, k ) * DPMIN( k + , r );
  102. first = false;
  103. }
  104. else
  105. res = min( res, DPMIN( l, k ) * DPMIN( k + , r ) );
  106. }
  107. }
  108. //printf( "dp[%d][%d]=%I64u\n", l, r, res );
  109. return res;
  110. }
  111.  
  112. int main()
  113. {
  114. int T;
  115. scanf( "%d", &T );
  116. while ( T-- )
  117. {
  118. scanf( "%s", str );
  119. chuli();
  120. memset( dpMax, , sizeof(dpMax) );
  121. memset( dpMin, , sizeof(dpMin) );
  122.  
  123. memset( vis, false, sizeof(vis) );
  124. LL maxx = DPMAX( , cntNum - );
  125.  
  126. memset( vis, false, sizeof(vis) );
  127. LL minn = DPMIN( , cntNum - );
  128.  
  129. printf( "%llu %llu\n", maxx, minn );
  130. }
  131. return ;
  132. }

SPOJ 364 Pocket Money 简单DP的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. hdu 2471 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=(  dp[n-1][m],dp[n][m-1],d[i][k ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

随机推荐

  1. autofac 的好博文

    https://www.cnblogs.com/neverc/p/4914091.html#e https://www.cnblogs.com/stulzq/p/8547839.html

  2. mysql常用命令添加外键主键约束存储过程索引

    数据库连接 mysql -u root -p123456 查看表 show databases 创建数据库设置编码 create table books character set utf8; 创建用 ...

  3. scrollHeight, scrollTop, clientHeight

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 1.Netty入门

    Netty入门 1.Netty介绍 (1)百度百科介绍: Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络 ...

  5. 修改SecureCRT默认会话字符集

    修改SecureCRT默认会话字符集 1.找到SecureCRT配置文件 Default.ini 2.修改Default修改为UTF-8 将S:"Output Transformer Nam ...

  6. java 基础词汇 必须 第九天

    Collection 集合 List 列表集合 Set 不重复集合 Linked 链表 Vector 线程安全集合 Hash 哈希值 tree 树型结构 Map 键值对集合 add 增加 remove ...

  7. 友盟分享小结 - iOS

    因之前都写在了 AppDelegate 类中,看起来过于臃肿,此次基于友盟分享重新进行了一次优化,此次分享内容基于已经成功集成 SDK 后 code 层级部分.注:此次分享基于 SDK 6.9.3,若 ...

  8. 牛客小白月赛2 D 虚虚实实 【欧拉图】【连通图】

    链接:https://www.nowcoder.com/acm/contest/86/D来源:牛客网 题目描述 震为雷,临危不乱,亨通畅达:巽为风,柔顺伸展,厚载万物. 震卦:洊雷,震,君子以恐惧修省 ...

  9. C#进阶学习笔记(个人整理)

    学习笔记 第一章: 一.回顾数组 1.变量 : 只能存储一个数据 2.数组 :存储固定大小的相同类型的数据 3.对象 : 存储多个相同/不同类型的数据 4.集合 : 特殊的容器,存储N个相同/不同类型 ...

  10. python__系统 : socket_UDP相关

    socket.socket() 可以创建一个套接字: from socket import * from threading import Thread udp_socket = None dest_ ...