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

B. Mathematical Curse(ACM-ICPC 2018 焦作賽區網路預賽,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}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 \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],…,aN, 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

題目來源

思路

n個格子,每個格子裡面有一個數,有m個運算子,你的初始值為k.

現在規定,你必須按照從1到n的順序去訪問每個格子,並且決定在當前格子中要不要進行運算,運算的方式為:你的當前值 = 你的當前值 你要使用的符號 格子裡面的值。你必須用完所有的符號,格子裡面的數可能為負,現在你需要走完這些格子,然後求一個最大值。

思路就是dp,從1到n,每個格子有兩種狀態,用符號和不用符號,我們記錄一個最大值和最小值直接dp即可。

dp[i][j]表示前i個格子,用了j個符號,所能達到的最大最小值。

首先初始化dp[i][0]k,然後初始化dp[1~m][1~m]的值為運算的結果。

然後轉移方程為:

dp[i][j].min = smin(dp[i - 1][j].min, calc(dp[i - 1][j - 1].min, a[i], s[j]), calc(dp[i - 1][j - 1].max, a[i], s[j]));
dp[i][j].max = smax(dp[i - 1][j].max, calc(dp[i - 1][j - 1].min, a[i], s[j]), calc(dp[i - 1][j - 1].max, a[i], s[j]));

程式碼

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e3 + 10;
ll a[N];
char s[20];
struct node
{
    ll max, min;
} dp[N][10];
ll calc(ll x, ll y, char ch)
{
    if (ch == '+')
        return x + y;
    else if (ch == '-')
        return x - y;
    else if (ch == '*')
        return x * y;
    else if (ch == '/')
        return x / y;
}
ll smin(ll x, ll y, ll z)
{
    return min(x, min(y, z));
}
ll smax(ll x, ll y, ll z)
{
    return max(x, max(y, z));
}

void solve()
{
    ll n, m, k;
    scanf("%lld%lld%lld", &n, &m, &k);
    for (ll i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    scanf("%s", s + 1);
    for (ll i = 0; i <= n; i++)
        dp[i][0].max = dp[i][0].min = k;
    for (ll i = 1; i <= m; i++)
        dp[i][i].max = dp[i][i].min = calc(dp[i - 1][i - 1].max, a[i], s[i]);
    for (ll j = 1; j <= m; j++)
    {
        for (ll i = j + 1; i <= n; i++)
        {
            dp[i][j].min = smin(dp[i - 1][j].min, calc(dp[i - 1][j - 1].min, a[i], s[j]), calc(dp[i - 1][j - 1].max, a[i], s[j]));
            dp[i][j].max = smax(dp[i - 1][j].max, calc(dp[i - 1][j - 1].min, a[i], s[j]), calc(dp[i - 1][j - 1].max, a[i], s[j]));
        }
    }
    printf("%lld\n", dp[n][m].max);
}
int main()
{
    //freopen("in.txt", "r", stdin);
    ll t;
    scanf("%lld", &t);
    while (t--)
        solve();
    return 0;
}