1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽 B Mathematical Curse —— dp

ACM-ICPC 2018 焦作賽區網路預賽 B Mathematical Curse —— dp

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}i th room, there is a wizard who has a resentment value of a[i]a[i]. The prince has MM curses, the j^{th}j th 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}j th curse in the i^{th}i th 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 \le T \le 1000)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 \le N \le 1000), M(1 \le M \le 5)N(1≤N≤1000),M(1≤M≤5) and K(-1000 \le K \le 1000K(−1000≤K≤1000), the second line contains NN non-zero integers: a[1], a[2], …, a[N](-1000 \le a[i] \le 1000)a[1],a[2],…,a

N, and the third line contains MM characters: f[1], f[2], …, f[M](f[j] =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 題目連結:https://nanti.jisuanke.com/t/31711 題意: t組樣例,n,m,k接下來有n個數,m次操作,初值為k 按順序執行+或-或*或/,問你執行了m次操作之後k的最大值是多少 題解: 那麼我們只要開二維的dp陣列,dpbig[i][j]代表到第i個數執行了j次操作最大的值是多少 但是要注意這裡的*可能是乘負數會更優,比如說10 10 -1000 -1000 10 10 10 的時候,乘上2個-1000肯定比其他的乘起來要大。所以我們還需要開一個dpsmall陣列記錄最小值,那麼算dpbig的時候就有一種可能是從dpsmall裡乘一個負數得到最大的答案。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=(int)1e9+7;
const int maxn=1005;
ll a[maxn],dpbig[maxn][10],dpsmall[maxn][10];
char op[maxn];
int n,m,k;
ll deal(ll x,char op,ll y){
    if(op=='+') return x+y;
    if(op=='-') return x-y;
    if(op=='*') return x*y;
    if(op=='/') return x/y;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        scanf("%s",op+1);
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dpbig[i][j]=-1e18-5;
            }
        }
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dpsmall[i][j]=1e18+5;
            }
        }
        for(int i=0;i<=n;i++){
            dpbig[i][0]=dpsmall[i][0]=k;
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=min(i,m);j++){
                if(a[i]>0||op[j]=='-'||op[j]=='+'){
                    ll now=deal(dpbig[i-1][j-1],op[j],a[i]);
                    dpbig[i][j]=max(dpbig[i][j],max(dpbig[i-1][j],now));
                    now=deal(dpsmall[i-1][j-1],op[j],a[i]);
                    dpsmall[i][j]=min(dpsmall[i][j],min(dpsmall[i-1][j],now));
                }
                else {
                    ll now=deal(dpbig[i-1][j-1],op[j],a[i]);
                    dpsmall[i][j]=min(dpsmall[i][j],min(dpsmall[i-1][j],now));
                    now=deal(dpsmall[i-1][j-1],op[j],a[i]);
                    dpbig[i][j]=max(dpbig[i][j],max(dpbig[i-1][j],now));
                }
            }
        }
        ll ans=-1e18-5;
        for(int i=1;i<=n;i++){
            ans=dpbig[i][m];
        }
        printf("%lld\n",ans);
    }
    return 0;
}