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

输出用%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

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm> #define LL unsigned long long int using namespace std; const int MAXN = ; int cntNum, cntOp;
char str[MAXN];
char op[MAXN];
LL num[MAXN];
bool vis[MAXN][MAXN];
LL dpMax[MAXN][MAXN];
LL dpMin[MAXN][MAXN]; void show()
{
printf("All nums: \n");
for ( int i = ; i < cntNum; ++i )
printf( "%I64d ", num[i] ); printf("\nAll Ops:\n");
for ( int i = ; i < cntOp; ++i )
putchar( op[i] );
puts("");
return;
} void chuli()
{
cntNum = cntOp = ;
int len = strlen(str);
for ( int i = ; i < len; ++i )
{
LL tmp = ;
while ( i < len && isdigit( str[i] ) )
{
tmp = tmp * + ( str[i] - '' );
++i;
}
num[ cntNum++ ] = tmp;
if ( i < len )
op[ cntOp++ ] = str[i];
} //show();
return;
} LL DPMAX( int l, int r )
{
LL &res = dpMax[l][r];
if ( vis[l][r] ) return res; vis[l][r] = true;
if ( l == r ) return res = num[l];
res = ; for ( int k = l; k < r; ++k )
{
if ( op[k] == '+' )
res = max( res, DPMAX( l, k ) + DPMAX( k + , r ) );
else if ( op[k] == '*' )
res = max( res, DPMAX( l, k ) * DPMAX( k + , r ) );
}
//printf( "dp[%d][%d]=%I64u\n", l, r, res ); return res;
} LL DPMIN( int l, int r )
{
LL &res = dpMin[l][r];
if ( vis[l][r] ) return res; vis[l][r] = true;
if ( l == r ) return res = num[l];
bool first = true; for ( int k = l; k < r; ++k )
{
if ( op[k] == '+' )
{
if ( first )
{
res = DPMIN( l, k ) + DPMIN( k + , r );
first = false;
}
else
res = min( res, DPMIN( l, k ) + DPMIN( k + , r ) );
}
else if ( op[k] == '*' )
{
if ( first )
{
res = DPMIN( l, k ) * DPMIN( k + , r );
first = false;
}
else
res = min( res, DPMIN( l, k ) * DPMIN( k + , r ) );
}
}
//printf( "dp[%d][%d]=%I64u\n", l, r, res );
return res;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%s", str );
chuli();
memset( dpMax, , sizeof(dpMax) );
memset( dpMin, , sizeof(dpMin) ); memset( vis, false, sizeof(vis) );
LL maxx = DPMAX( , cntNum - ); memset( vis, false, sizeof(vis) );
LL minn = DPMIN( , cntNum - ); printf( "%llu %llu\n", maxx, minn );
}
return ;
}

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. spfa判负权边

    spfa判负环 如果一个点在spfa中被入队了大于n次 那么,我们就能肯定,有负环出现. 因为一个点入队时,他肯定被更新了一次. 所以........ 如果不存在负权环.这个点最多被更新节点数次 我们 ...

  2. 单源最短路模板(dijkstra)

    单源最短路(dijkstra算法及堆优化) 弱化版题目链接 n^2 dijkstra模板 #include<iostream> #include<cstdio> #includ ...

  3. pooling

    转自:http://www.gageet.com/2014/09182.php 本文部分参考了:http://www.zhihu.com/question/23437871 卷积层是对图像的一个邻域进 ...

  4. 一篇RxJava友好的文章(三)

    组合操作符 继上一篇讲述了过滤操作符,这一篇讲述组合操作符,组合操作符可用于组合多个Observable.组合操作符相对于过滤操作符要复杂很多,也较难以理解,需要花费时间去看文档查资料,写demo才能 ...

  5. OCCI结果集(ResultSet)性能优化

    对于ResultSet类中的next()方法,默认是一次检索一行数据,及一次检索执行一次网络往返,当结果集数量大时,效率低:对此OCCI提供了几种改善方法,即:在一次网络往返返回多行数据. 1. 通过 ...

  6. 【例题收藏】◇例题·V◇ Gap

    ◇例题·V◇ Gap 搜索训练开始了……POJ的数据比ZOJ强多了!!看来不得不写正解了 +传送门+ ◇ 题目 <简要翻译> 有一个四行九列的矩阵——在第1~4行.2~8列上填上数字 11 ...

  7. 【模板时间】◆模板·I◆ 倍增计算LCA

    [模板·I]LCA(倍增版) 既然是一篇重点在于介绍.分析一个模板的Blog,作者将主要分析其原理,可能会比较无趣……(提供C++模板) 另外,给reader们介绍另外一篇非常不错的Blog(我就是从 ...

  8. 微信公众号支付java版本

    回调函数 @RequestMapping("/toPay") public String toPay(HttpServletRequest request, HttpServlet ...

  9. jQuery-laye插件实现 弹框编辑,异步验证,form验证提交

    代码中用到了 jQuery的ajax异步处理,each()循环,submit()页面验证提交form表单,prepend()追加标签,laye插件的弹框效果(如有其它弹框效果可参考官网:http:// ...

  10. php图片上传旋转压缩方法

    用到php的exif扩展,需要开启exif 在php.ini文件中去掉exif组件的注释 extension=php_mbstring.dll //要放在php_exif.dll前面让它先加载 ext ...