问题 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 N rooms from the place where he was imprisoned to the exit of the castle. In the i^th room, there is a wizard who has a resentment value of a[i]. The prince has M curses, the j^th curse is f[j], and f[j] represents one of the four arithmetic operations, namely addition('+'), subtraction('-'), multiplication('*'), and integer division (' / '). The prince’s initial resentment value is K. 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] with the wizard's resentment value. That is, if the prince eliminates the j^th curse in the i^th room, then his resentment value will change from x to (x f[j] a[i]), for example, when x=1, a[i]=2, f[j]='+', then x will become 1+2=3.
Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1] to a[N] in order and cannot turn back. He must also eliminate the f[1] to f[M] curses in order(It is guaranteed that N≥M). What is the maximum resentment value that the prince may have when he leaves the castle?
输入
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) and K(-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 M characters: f[1], f[2], … , f[M] (f[j] 〖= 〗^' +^','-^','*',' / '), with no spaces in between.
输出
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
简单的DP,虽然挺简单,但是我不会。
dp[i][j]存入的是第i个房间执行第j次操作后的怒气值。因为存在乘法和除法并且有负数,所以我们对于最大值与最小值都需要维护,防止负负得正,变成最大。
对于我们的每一次dp[i][j]都应该根据上一个房间上一次操作来改变,因为题目说了,必须按照顺序。那么又因为操作是必须全部选择的,而房间是可以跳跃选择的,所以我们每一次操作之前都应该把上一个房间的操作给传递过来。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=0x3f3f3f;
struct node
{
int mx;
int mn;
}dp[1010][10];
int a[1010];
char s[10];
signed main()
{
int T;
cin>>T;
while(T--)
{
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
cin>>s;
for(int i=0;i<1010;i++)
for(int j=0;j<10;j++)
{
dp[i][j].mx=-inf;
dp[i][j].mn=inf;
}
for(int i=0;i<=n;i++)dp[i][0].mx=dp[i][0].mn=k;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)dp[i][j]=dp[i-1][j];
for(int j=1;j<=m;j++)
{
if(j>i)break;//因为例如第一个房间不可能执行第1个以后的操作
if(s[j-1]=='+')
{
int f1=dp[i-1][j-1].mx+a[i];
int f2=dp[i-1][j-1].mn+a[i];
dp[i][j].mx=max(dp[i][j].mx,f1);
dp[i][j].mn=min(dp[i][j].mn,f2);
}
else if(s[j-1]=='-')
{
int f1=dp[i-1][j-1].mx-a[i];
int f2=dp[i-1][j-1].mn-a[i];
dp[i][j].mx=max(dp[i][j].mx,f1);
dp[i][j].mn=min(dp[i][j].mn,f2);
}
else if(s[j-1]=='*')
{
int f1=dp[i-1][j-1].mx*a[i];
int f2=dp[i-1][j-1].mn*a[i];
if(f1<f2)swap(f1,f2);
dp[i][j].mx=max(dp[i][j].mx,f1);
dp[i][j].mn=min(dp[i][j].mn,f2);
}
else
{
int f1=dp[i-1][j-1].mx/a[i];
int f2=dp[i-1][j-1].mn/a[i];
if(f1<f2)swap(f1,f2);
dp[i][j].mx=max(dp[i][j].mx,f1);
dp[i][j].mn=min(dp[i][j].mn,f2);
}
}
}
cout<<dp[n][m].mx<<endl;
}
}