ACM-ICPC 2018 焦作网络赛 B. Mathematical Curse (dp)

版权声明:本文为蒟蒻原创文章,转载请注明出处哦~ https://blog.csdn.net/a54665sdgf/article/details/82748274

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 MMcurses, 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],...,a[N](−1000≤a[i]≤1000), 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个运算符(n>=m),给你一个初始的数k,让你从n个数中选择m个数分别用这m个运算符来对k进行处理,要求选择的数的顺序要与运算符的顺序一致,求能够得到的最大值。

解法:由于下一个阶段的最大值只与上一个阶段的最大值或最小值有关,最小值也是如此,所以只对最大值或最小值做dp即可。

可设d[i][j][1]为前i个数用j个运算符能凑成的最大值,d[i][j][0]为最小值。

注意边界处理。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000+10;
const int M=5+2;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll d[N][M][2];
char ch[M];
ll a[N],k,ans;
int n,m;

ll f(ll x,char c,ll y)
{
    if(c=='+')return x+y;
    if(c=='-')return x-y;
    if(c=='*')return x*y;
    if(c=='/')return x/y;
}

void solve()
{
    d[0][0][0]=d[0][0][1]=k;
    for(int i=1; i<=n; ++i)
        for(int j=min(i,m); j>=0; --j)
        {
            if(j==0)//此状态只能由d[i-1][j]继承而来
            {
                d[i][j][0]=d[i-1][j][0];
                d[i][j][1]=d[i-1][j][0];
            }
            else if(j==i)//此状态只能由d[i-1][j-1]转移而来
            {
                d[i][j][0]=min(f(d[i-1][j-1][0],ch[j-1],a[i-1]),f(d[i-1][j-1][1],ch[j-1],a[i-1]));
                d[i][j][1]=max(f(d[i-1][j-1][0],ch[j-1],a[i-1]),f(d[i-1][j-1][1],ch[j-1],a[i-1]));
            }
            else//此状态既可以由d[i-1][j]继承而来,也可以由d[i-1][j-1]转移而来
            {
                d[i][j][0]=min(d[i-1][j][0],min(f(d[i-1][j-1][0],ch[j-1],a[i-1]),f(d[i-1][j-1][1],ch[j-1],a[i-1])));
                d[i][j][1]=max(d[i-1][j][1],max(f(d[i-1][j-1][0],ch[j-1],a[i-1]),f(d[i-1][j-1][1],ch[j-1],a[i-1])));
            }
        }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%lld",&n,&m,&k);
        for(int i=0; i<n; ++i)scanf("%lld",&a[i]);
        scanf("%s",ch);
        solve();
        printf("%lld\n",d[n][m][1]);
    }
    return 0;
}

或者也可以直接用一维来做:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000+10;
const int M=5+2;
const ll INF=0x3f3f3f3f3f3f3f3f;
ll Max[M],Min[M];
char ch[M];
ll a[N],k,ans;
int n,m;

ll f(ll x,char c,ll y)
{
    if(c=='+')return x+y;
    if(c=='-')return x-y;
    if(c=='*')return x*y;
    if(c=='/')return x/y;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%lld",&n,&m,&k);
        for(int i=0; i<n; ++i)scanf("%lld",&a[i]);
        scanf("%s",ch);
        memset(Max,~0x3f,sizeof Max);
        memset(Min,0x3f,sizeof Min);
        Max[0]=Min[0]=k;
        for(int i=1; i<=n; ++i)
            for(int j=min(i,m); j>=1; --j)
            {
                Min[j]=min(Min[j],min(f(Min[j-1],ch[j-1],a[i-1]),f(Max[j-1],ch[j-1],a[i-1])));
                Max[j]=max(Max[j],max(f(Min[j-1],ch[j-1],a[i-1]),f(Max[j-1],ch[j-1],a[i-1])));
            }
        printf("%lld\n",Max[m]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a54665sdgf/article/details/82748274