SPOJ 364 Pocket Money 简单DP
跟矩阵链乘同类型的题……
输出用%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的更多相关文章
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- 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 ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
随机推荐
- R语言网络爬虫学习 基于rvest包
R语言网络爬虫学习 基于rvest包 龙君蛋君:2015年3月26日 1.背景介绍: 前几天看到有人写了一篇用R爬虫的文章,感兴趣,于是自己学习了.好吧,其实我和那篇文章R语言爬虫初尝试-基于RVES ...
- Python 3 collections.defaultdict() 与 dict的使用和区别
综述: 这里的defaultdict(function_factory)构建的是一个类似dictionary的对象,其中keys的值,自行确定赋值,但是values的类型,是function_fact ...
- 缓冲区溢出实战教程系列(二):dev c++编译汇编代码
小伙伴们对我上一篇文章的反应完全出乎了我的意料,感谢大家对我的支持和认可.接下来我会精心的把这一系列课程设计好,尽量详细的展示给大家.上篇文章我列举了一个缓冲区溢出的小例子,并提到了dev c++.o ...
- System.Web.Caching.Cache
此类是利用缓存来保存信息的.可以把一些稳定的数据,不会随用户而改变的信息利用Cache保存起来,可以优化网站的速度. Cache辅助类已上传:GitHub Cache和Session,cookie的区 ...
- Sass 语法格式及编译
一.sass语法格式 这里说的 Sass 语法是 Sass 的最初语法格式,他是通过 tab 键控制缩进的一种语法规则,而且这种缩进要求非常严格.另外其不带有任何的分号和大括号.常常把这种格式称为 S ...
- linux命令详解-useradd,groupadd
linux命令详解-useradd,groupadd 我们在linux命令行中输入useradd: Options: -b, --base-dir BASE_DIR base direc ...
- Objective-c 单例设计模式
Objective-c 单例设计模式 一.什么是单例模式:(Singleton) 单例模式的意图是是的类的对象成为系统中唯一的实例,提供一个访问点,供客户类共享资源. 二.什么情况下使用 ...
- Q&A - Nginx是做什么的?tomcat结合Nginx使用小结
相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...
- v-cloak
v-cloak 不需要表达式 用法: 这个指令保持在元素上直到关联实例结束编译.和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Must ...
- XStream 工具类 [ XmlUtil ]
pom.xml <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId> ...