Mathematical Curse(线性dp)


问题 B: Mathematical Curse

问题 B: Mathematical Curse

时间限制: 1 Sec  内存限制: 128 MB
提交: 152  解决: 35
[提交] [状态] [命题人:admin]

题目描述

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

dp1[i][j]:前i个房间用了j个符号的最大值

dp2[i][j]:前i个房间用了j个符号的最小值

和01背包差不多~~~

为什么还要存最小值呢?

加减都用最大的就好了,但是乘除如果这个数是个负的,那么用前面最小的参与运算可能更优(负负得正)

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
const int mod=1000000007;
ll dp1[1005][10];
ll dp2[1005][10];
ll a[1005];
int n,m,k;
char s[10];
int main(){
    int T;
    scanf("%d",&T);
    while(T--){ 
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
        scanf("%s",s+1);
        for(int i=0;i<=n;i++) dp1[i][0]=k,dp2[i][0]=k;
        for(int i=0;i<=m;i++){
            for(int j=i+1;j<=m;j++){
                dp1[i][j]=-1e16;
                dp2[i][j]=1e16;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=i&&j<=m;j++){
                if(s[j]=='+'){
                    dp1[i][j]=max(dp1[i-1][j-1]+a[i],dp1[i-1][j]);
                    dp2[i][j]=min(dp2[i-1][j-1]+a[i],dp2[i-1][j]);
                }
                if(s[j]=='-'){
                    dp1[i][j]=max(dp1[i-1][j-1]-a[i],dp1[i-1][j]);
                    dp2[i][j]=min(dp2[i-1][j-1]-a[i],dp2[i-1][j]);
                }
                if(s[j]=='*'){
                    dp1[i][j]=max(dp1[i-1][j],max(dp1[i-1][j-1]*a[i],dp2[i-1][j-1]*a[i]));
                    dp2[i][j]=min(dp2[i-1][j],min(dp1[i-1][j-1]*a[i],dp2[i-1][j-1]*a[i]));
                }
                if(s[j]=='/'){
                    dp1[i][j]=max(dp1[i-1][j],max(dp1[i-1][j-1]/a[i],dp2[i-1][j-1]/a[i]));
                    dp2[i][j]=min(dp2[i-1][j],min(dp1[i-1][j-1]/a[i],dp2[i-1][j-1]/a[i]));
                }
            }   
        }
        printf("%lld\n",dp1[n][m]);
    }
    
    return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值