SPOJ 364 Pocket Money 简单DP
2024-08-31 00:39:15
跟矩阵链乘同类型的题……
输出用%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 ;
}
最新文章
- noi 6045 开餐馆
- java堆内存与栈内存
- Dive into python 实例学python (1) —— 函数和测试
- 51nod1442 士兵的旅行
- 面经-csdn
- python 读取机器信息
- 小学生之Java中的异常
- C++中多重继承构造函数执行顺序
- Android 实时文件夹
- PHP中判断输入验证码是否一致
- ESXi5.0误删除虚拟机还有办法恢复吗?答案是可以!
- 数据降维之多维缩放MDS(Multiple Dimensional Scaling)
- 【题解】JSOIWC2019 Round1
- 64位Win7系统WMware安装Mac OS
- memcache分布式布置方案
- MySQL(安装,服务,创建用户及授权)
- MYSQL 复制详解
- [agc011E]Increasing Numbers-[思考题]
- linux screen 命令详解(转载)
- Stat3—因子分析(Factor Analysis)