标签:
题意是这样,给出一个运算符只有+跟*,数字都在1到9之间的算式,要你加入一对括号,使得算式的结果尽可能的大,保证最多十五个乘号。很显然,若要让加入的括号能够影响原本运算的结果,必然是要影响乘法,那么加入的这对括号中必然至少有一个跟乘号是相邻的,恰好乘号的数目很小,那么直接枚举括号的位置即可,每次算出当前解更新ans即可。
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
ll x[3000];
bool s[3000];
ll work(int l,int r,int l1,int r1)
{
ll ans=0;
for(int i=l;i<=r;i++)
{
if(i==l1)
i=r1;
if(s[i])
ans+=x[i];
else
{
ll t=1;
for(int j=i;j<=r;j++)
{
if(j==l1)
j=r1;
t*=x[j];
if(s[j]||j==r)
{
i=j;
break;
}
}
ans+=t;
}
}
return ans;
}
int main()
{
int n=0,m=0;
for(int i=0;;i++)
{
char c=getchar();
if(c=='\n')
break;
if(i&1)
s[m++]=(c=='+');
else
x[n++]=c-'0';
}
ll ans=work(0,n-1,-1,-1);
for(int i=0;i<m;i++)
if(!s[i])
{
for(int j=0;j<=i;j++)
{
ll t=work(j,i,-1,-1);
swap(t,x[i]);
ans=max(ans,work(0,n-1,j,i));
swap(t,x[i]);
}
for(int j=i+1;j<n;j++)
{
ll t=work(i+1,j,-1,-1);
swap(t,x[j]);
ans=max(ans,work(0,n-1,i+1,j));
swap(t,x[j]);
}
}
cout<<ans;
}
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.
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.
In the first line print the maximum possible value of an expression.
3+5*7+8*4
303
2+3*5
25
3*4*5
60
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).
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 552 E Vanya and Brackets
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/46974339