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 行有 ...
随机推荐
- 2017.2.20 activiti实战--第二章--搭建Activiti开发环境及简单示例(一)搭建开发环境
学习资料:<Activiti实战> 第一章 认识Activiti 2.1 下载Activiti 官网:http://activiti.org/download.html 进入下载页后,可以 ...
- iOS 应用内跳转到系统设置
在iOS5下面版本号使用下面方法:[IOS5.1+之后不能使用此方法.iOS8的跳转方法已找到见下方,iOS7的正在摸索,欢迎大家给出观点意见] 通过URL Scheme的方式打开内置的Setting ...
- GTD实用指南
以前通过余弦大牛博客接触到了GTD, 后来我自己接触之后呢, 我是非常讨厌GTD的, 因为太功利化了 反人类 我还是比较懒得··· 可是最近事情真的比较多,不得不做GTD了 = = 郁闷! 时间管理 ...
- php猴子吃桃
<?php header("content-type:text/html;charset=utf-8"); /* 有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个! 以后 ...
- 如何让DIV居中
总结:text-align:center;对三中浏览器而言,都具有文字/行内元素的嵌套式居中,或者说继承式的居中,只要外面的容器设置了这个属性,那么他内部的所有元素都具有这个属性(意思是,虽然这个属性 ...
- robotframework使用之元素定位动态ID方法
转自: http://blog.csdn.net/u011757108/article/details/53418671 一个弹出框所有元素ID竟然的动态的,关闭后再打开,里面的ID又变! 如下图: ...
- 介绍JSON
0x00 介绍JSON 介绍JSON :http://www.json.org/json-zh.html Introducing JSON :http://www.json.org/
- HDU 2112 HDU Today(STL MAP + Djistra)
题目链接:HDU Today 立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map 题目非常水,可是错了N遍.手贱了.本题不优点理的就是把地名转化为数字 #include <i ...
- AI生万物,新世界的大门已敞开
四月是万物复苏的时节,一年一度的GMIC全球移动互联网大会也在这个时间如期而至,在4月26日-28日的会议期间,有超过三百位行业专家进行了精彩的演讲,更有数万名现场观众感受到思维碰撞迸发出的火花. 作 ...
- LightOJ 1068 Investigation (数位dp)
problem=1068">http://www.lightoj.com/volume_showproblem.php?problem=1068 求出区间[A,B]内能被K整除且各位数 ...