B. Mathematical Curse
A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to escape the castle.
There are NN rooms from the place where he was imprisoned to the exit of the castle. In the i^{th}ith room, there is a wizard who has a resentment value of a[i]a[i]. The prince has MM curses, the j^{th}jth curse is f[j]f[j], and f[j]f[j] represents one of the four arithmetic operations, namely addition('+'
), subtraction('-'
), multiplication('*'
), and integer division('/'
). The prince's initial resentment value is KK. Entering a room and fighting with the wizard will eliminate a curse, but the prince's resentment value will become the result of the arithmetic operation f[j]f[j] with the wizard's resentment value. That is, if the prince eliminates the j^{th}jth curse in the i^{th}ith room, then his resentment value will change from xx to (x\ f[j]\ a[i]x f[j] a[i]), for example, when x=1, a[i]=2, f[j]=x=1,a[i]=2,f[j]='+'
, then xx will become 1+2=31+2=3.
Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1]a[1] to a[N]a[N] in order and cannot turn back. He must also eliminate the f[1]f[1] to f[M]f[M] curses in order(It is guaranteed that N\ge MN≥M). What is the maximum resentment value that the prince may have when he leaves the castle?
Input
The first line contains an integer T(1≤T≤1000), which is the number of test cases.
For each test case, the first line contains three non-zero integers:N(1≤N≤1000),M(1≤M≤5) andK(−1000≤K≤1000), the second line contains N non-zero integers: a[1], a[2], ..., a[N](−1000≤a[i]≤1000), and the third line contains MM characters:f[1],f[2],...,f[M](f[j]='+','-','*','/'
, with no spaces in between.)
Output
For each test case, output one line containing a single integer.
样例输入
3 2 1 5 2 3 / 3 2 1 1 2 3 ++ 4 4 5 1 2 3 4 +-*/
样例输出
2 6 3
有n个数和m个运算符,按顺序选m个数进行运算,初值为k,问最后能得到的最大值是多少。
dp[i][j]表示选到了第i个数时用了j个运算符,观察发现,一个数只能由它前一个状态的最大值或最小值转移过来(因为乘上一个负数会使最小的数变最大),所以我们同时维护最大最小。
要开long long!!!
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dpmax[1005][6],dpmin[1005][6]; //保存前i个数用了j个运算符的最大值最小值
int room[1005];
char s[10];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(dpmax,0,sizeof(dpmax));
memset(dpmin,0,sizeof(dpmin));
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++) scanf("%d",&room[i]);
scanf("%s",s+1);
dpmax[0][0]=dpmin[0][0]=k;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(j==0) {dpmax[i][j]=dpmin[i][j]=k;continue;}
ll temp1=dpmax[i-1][j-1],temp2=dpmin[i-1][j-1];
ll maxx,minn;
if(s[j]=='+')
{
maxx=max(temp1+room[i],temp2+room[i]);
minn=min(temp1+room[i],temp2+room[i]);
}
else if(s[j]=='-')
{
maxx=max(temp1-room[i],temp2-room[i]);
minn=min(temp1-room[i],temp2-room[i]);
}
else if(s[j]=='*')
{
maxx=max(temp1*room[i],temp2*room[i]);
minn=min(temp1*room[i],temp2*room[i]);
}
else
{
maxx=max(temp1/room[i],temp2/room[i]);
minn=min(temp1/room[i],temp2/room[i]);
}
dpmax[i][j]=max(dpmax[i-1][j],maxx);
dpmin[i][j]=min(dpmin[i-1][j],minn);
if(i==j)
{
dpmax[i][j]=maxx;
dpmin[i][j]=minn;
}
}
}
printf("%lld\n",dpmax[n][m]);
}
return 0;
}