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 行有 ...
随机推荐
- [java面试]关于多态性的理解
执行时多态性是面向对象程序设计代码重用的一个最强大机制.Java多态性的概念也能够被说成"一个接口.多个方法".Java实现执行时多态性的基础是动态方法调度,它是一种在执行时而不是 ...
- 使用yum方式在centOS上安装mysql
1.操作系统及MySQL版本 1.1 操作系统版本 CentOS release 6.5 (Final) 1.2 MySQL版本 mysql-5.1.73-3.el6_5.x86_64mysql-li ...
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...
- windows的iis做后门,隐藏访问,无日志
windows下的iis5/iis6做后门,隐藏访问,不留访问记录或者不留日志 好不容易攻下一台Windows2000/2003 IIS服务器,你一定会想,怎样才能长期占有这个“肉鸡”呢?聪明的你肯定 ...
- mbr 备份
MBR共512字节 (1) 第1-446字节:调用操作系统的机器码. (2) 第447-510字节:分区表(Partition table). (3) 第511-512字节:主引导记录签名(0x55和 ...
- 编译-O 选项对性能提升作用
GCC -O 选项 这个选项控制所有的优化等级.使用优化选项会使编译过程耗费更多的时间,并且占用更多的内存,尤其是在提高优化等级的时候. -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. ...
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)
再用爬虫爬取数据的时候报错:[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661) 好多博客我看都说是:网站证书 ...
- 在diy的文件系统上创建文件的流程
[0]README 0.1) source code are from orange's implemention of a os , and for complete code , please v ...
- ios 添加全屏返回手势
1 建立导航控制器 2.导航控制器添加如下代码 - (void)viewDidLoad { [super viewDidLoad]; id target = self.interactivePopGe ...
- python 基础 2.7 range与xrange的区别
#/usr/bin/python #coding=utf-8 #@Time :2017/10/25 19:22 #@Auther :liuzhenchuan #@File :range与xrange的 ...