CodeForces - 552E Vanya and Brackets —— 加与乘运算的组合
题目链接:https://vjudge.net/contest/224393#problem/E
Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to 9, and sign
represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
Input
The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
Output
In the first line print the maximum possible value of an expression.
Examples
3+5*7+8*4
303
2+3*5
25
3*4*5
60
Note
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
题意:
给出一个只有加法和乘法的算式,且数字的范围为1~9,算式里面没有括号。问:怎样加一对括号,使得算式的结果最大?
题解:
1.比划一下,可发现规律:括号加在两‘+’中间,最终结果没有改变;括号加在‘+’和‘*’之间,可使结果变大,但不一定最优。只有当括号加在两'*'之间时,结果是最大。
2.题目规定了‘*’不会超过15个,所以可直接枚举左右括号的放置位置,然后求出算式的值,取最大值即可。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; char a[];
LL s[MAXN], len;
int pos[MAXN]; LL solve(int l, int r)
{
LL sum = , t1 = , t2 = , t3 = ;
int top = ; if(<=l) //左边
{
s[top++] = a[]-'';
for(int i = ; i<=l-; i+=)
{
if(a[i]=='*') s[top-] = 1LL*s[top-]*(a[i+]-'');
else if(a[i]=='+') s[top++] = (a[i+]-'');
}
t1 = s[--top];
while(top) sum += s[--top];
} //中间:
s[top++] = a[l+]-'';
for(int i = l+; i<=r-; i+=)
{
if(a[i]=='*') s[top-] = 1LL*s[top-]*(a[i+]-'');
else if(a[i]=='+') s[top++] = (a[i+]-'');
}
t2 = ;
while(top) t2 += s[--top]; // 右边:
if(r<=len-)
{
s[top++] = a[r+]-'';
for(int i = r+; i<=len-; i+=)
{
if(a[i]=='*') s[top-] = 1LL*s[top-]*(a[i+]-'');
else if(a[i]=='+') s[top++] = (a[i+]-'');
}
while(top>) sum += s[--top];
t3 = s[--top];
}
sum += 1LL*t1*t2*t3;
return sum;
} int main()
{
while(scanf("%s",a+)!=EOF)
{
int cnt = ;
pos[++cnt] = ;
len = strlen(a+);
for(int i = ; i<=len; i++)
if(a[i]=='*') pos[++cnt] = i;
pos[++cnt] = len+; LL sum = ;
for(int l = ; l<=cnt; l++) //枚举左右括号
for(int r = l+; r<=cnt; r++)
sum = max(sum, solve(pos[l], pos[r])); cout<<sum<<endl;
}
}
CodeForces - 552E Vanya and Brackets —— 加与乘运算的组合的更多相关文章
- CodeForces - 552E Vanya and Brackets
Vanya and Brackets Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...
- Codeforces 552E Vanya and Brackets(枚举 + 表达式计算)
题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左 ...
- Codeforces 552E - Vanya and Brackets【表达式求值】
给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...
- Vanya and Brackets
Vanya and Brackets Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- Codeforces 677D Vanya and Treasure 暴力+BFS
链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 552 E. Vanya and Brackets 表达式求值
题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...
- codeforces 492C. Vanya and Exams 解题报告
题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n, r, avg.然后有 n 行,每行有两个数:第 i 行有 ...
随机推荐
- Windows下批处理命令启动项目bat脚本
文件env.cfg #server name SERVER_NAME=ActivitiService #JDK Home JDK_HOME= #Main MAIN_CLASS=com.nbtv.com ...
- linux中shell脚本中系统预先定义的变量
$0:脚本名称: $*:所有参数: $$:当前进程或者脚本的PID号: $!:后台运行的最后一个进程的PID号: $?:用于返回上一个命令是否成功.成功0,否则为非零: $#:参数个数: $@:所有参 ...
- breakpoints && lldb  && chisel 的使用
Breakpoints BreakPoint分类 breakpoint也是有分类的.我这里的文章内大致按使用的方式分为了 Normal Breakpoint,Exception Breakpoint, ...
- MAT使用及OOM分析
知识及工具推荐 1.Android资源监控工具 2.Android内存管理机制 http://blog.csdn.net/hexieshangwang/article/details/47188987
- Linux Unix shell 编程指南学习笔记(第四部分)
第十六章 shell脚本介绍 此章节内容较为简单,跳过. 第十七章 条件測试 test命令 expr命令 test 格式 test condition 或者 [ conditio ...
- Cocos2d-x学习笔记(四) 布景层的加入移除
布景层类也就是CCLayer类,每一个游戏场景中都能够有非常多层,每一层负责各自的任务.显示地图.显示人物等.同一时候层还是一个容器,能够放入文本.图片和菜单.构成游戏中一个个UI.这次将学习在场景中 ...
- vue sync
1.使用vue cli建立工程 2.在APP.vue中: <template> <div class="details"> <myComponent ...
- webstrom 代码工具(转http://www.cnblogs.com/tangdanni11/p/5149063.html)
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- iOS开发之解析XML格式数据
XML格式的数据是一种数据的传输格式.因为它方便编写.结构清晰,所以深受程序猿的喜爱,非常多人都喜欢使用XML格式数据传输或者作为程序的配置信息. 如今我将来实如今iOS中解析XML格式数据,语言使用 ...
- bit-map再显身手:test.txt中有42亿个无符号整数, 求不存在于test.txt中的最小无符号整数。限制: 可用内存为600MB.
先看看这个题目:test.txt中有42亿个无符号整数, 求不存在于test.txt中的最小无符号整数. 限制: 可用内存为600MB. 又是大数据. 看到42亿, 有灵感没? 要知道, 2的32次方 ...