CodeForces - 552E Vanya and Brackets

Vanya and Brackets

Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Description

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.

Sample Input

Input

3+5*7+8*4

Output

303

Input

2+3*5

Output

25

Input

3*4*5

Output

60

Hint

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).

Source

Codeforces Round #308 (Div. 2)

解题:暴力瞎搞,注意int溢出,傻逼逼的把栈写成int了。。。哎吸取教训

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 vector<int>pos;
 5 stack<LL>num;
 6 stack<char>op;
 7 LL calc(const string &str) {
 8     while(!num.empty()) num.pop();
 9     while(!op.empty()) op.pop();
10     for(int i = 0,slen = str.length(); i < slen; ++i) {
11         if(str[i] == ‘)‘) {
12             while(op.top() != ‘(‘) {
13                 LL tmp = num.top();
14                 num.pop();
15                 if(op.top() == ‘*‘) num.top() *= tmp;
16                 else if(op.top() == ‘+‘) num.top() += tmp;
17                 op.pop();
18             }
19             op.pop();
20             continue;
21         }
22         if(isdigit(str[i])) num.push(str[i] - ‘0‘);
23         else if(str[i] == ‘+‘ && !op.empty() && op.top() == ‘*‘) {
24             while(!op.empty() && op.top() == ‘*‘) {
25                 LL tmp = num.top();
26                 num.pop();
27                 num.top() *= tmp;
28                 op.pop();
29             }
30             op.push(str[i]);
31         } else op.push(str[i]);
32     }
33     while(!op.empty()) {
34         LL tmp = num.top();
35         num.pop();
36         if(op.top() == ‘*‘) num.top() *= tmp;
37         else if(op.top() == ‘+‘) num.top() += tmp;
38         op.pop();
39     }
40     return num.top();
41 }
42 int main() {
43     string str;
44     cin>>str;
45     pos.push_back(-1);
46     int slen = str.length();
47     for(int i = 1; i < slen; i += 2)
48         if(str[i] == ‘*‘) pos.push_back(i);
49     pos.push_back(slen);
50     slen = pos.size();
51     LL ret = INT_MIN;
52     for(int i = 0; i+1 < slen; ++i)
53         for(int j = i+1; j < slen; ++j) {
54             string s = str;
55             s.insert(pos[i]+1,1,‘(‘);
56             s.insert(pos[j]+1,1,‘)‘);
57             ret = max(ret,calc(s));
58         }
59     cout<<ret<<endl;
60     return 0;
61 }

时间: 2024-07-30 20:25:56

CodeForces - 552E Vanya and Brackets的相关文章

Codeforces 552E Vanya and Brackets(贪心 + 表达式计算)

题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左端或者某个乘号右边,右括号的位置肯定是最右段或者某个乘号左边. 而乘号最多只有15个,那么暴力枚举就可以了. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b);

Vanya and Brackets

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 552E Description Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to9, and sign  

codeforces 492E. Vanya and Field(exgcd求逆元)

题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走了n步后,x从0~n-1,y从0~n-1都访问过,但x,y不相同. 所以,x肯定要经过0点,所以我只需要求y点就可以了. i,j为每颗苹果树的位置,设在经过了a步后,i到达了0,j到达了M. 则有 1----------------------(i + b * dx) % n = 0 2------

Codeforces 492E Vanya and Field(拓展欧几里得)

题目链接:Codeforces 492E Vanya and Field 通过拓展欧几里得算法求出每个位置在移动过程中,在x为0时,y的位置.统计相应y坐标最多的即为答案. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e6 + 5; const int maxm = 1e5 + 5; typedef long long l

codeforces 552 E Vanya and Brackets

题意是这样,给出一个运算符只有+跟*,数字都在1到9之间的算式,要你加入一对括号,使得算式的结果尽可能的大,保证最多十五个乘号. 很显然,若要让加入的括号能够影响原本运算的结果,必然是要影响乘法,那么加入的这对括号中必然至少有一个跟乘号是相邻的,恰好乘号的数目很小,那么直接枚举括号的位置即可,每次算出当前解更新ans即可. #include<map> #include<string> #include<cstring> #include<cstdio> #i

CodeForces 552C Vanya and Scales

Vanya and Scales Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 552C Description Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some

CodeForces 380C Sereja and Brackets(扫描线+树状数组)

[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括号,其匹配的左括号是固定的, 我们保存每个右括号匹配的左括号位置, 对区间询问进行线扫描,将扫描的区间右端点及其之前所有的右括号对应的左括号位置做标记, 只要查询询问区间的标记个数就是答案,这个可以用树状数组维护. [代码] #include <cstdio> #include <algor

codeforces 492C. Vanya and Exams 解题报告

题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n,  r,  avg.然后有 n 行,每行有两个数:第 i 行有 ai 和 bi.表示如果写 bi 篇文章那么可以在 ai 这个分数上增加 1 分.可以增加好多次,但是前提是加完得到的分数不能超过 r.要使得 n 个exam 的分数平均分至少达到avg时需要写的最少文章是多少篇. 解决方法很简单,贪心即可. 我们当然希望写的文章越少越好,所以先对文章从小到大排序.

codeforces 492D Vanya and Computer Game(额。。。数学题?水题吧)

传送门:点击打开链接 题目大意: 有2个人在打怪,攻击频率分别是x,y.小怪有num点血.问死的时候是谁打死的.如果同时出手 输出Both. 解题思路: 在一秒内考虑这个问题(一秒是循环节). 假设攻击时刻的分母x*y.那么容易得到每个人的攻击时刻(在一秒内). 然后如果有一个时刻有重复.那么肯定是2个人同时在打. 排个序就好了. 这是D题么...我觉得最多是C题难度,吐槽一下... #include <cstdio> #include <vector> #include <