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

ACM-ICPC 2018 焦作网络预赛 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 NNN rooms from the place where he was imprisoned to the exit of the castle. In the ithi^{th}ith room, there is a wizard who has a resentment value of a[i]a[i]a[i]. The prince has MMM curses, the jthj^{th}jth curse is f[j]f[j]f[j], and f[j]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 KKK. 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]f[j] with the wizard’s resentment value. That is, if the prince eliminates the jthj^{th}jth curse in the ithi^{th}ith room, then his resentment value will change from xxx to (x f[j] a[i]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]=x=1,a[i]=2,f[j]=’+’, then xxx will become 1+2=31+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]a[1] to a[N]a[N]a[N] in order and cannot turn back. He must also eliminate the f[1]f[1]f[1] to f[M]f[M]f[M] curses in order(It is guaranteed that N≥MN\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≤T≤1000)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≤N≤1000),M(1≤M≤5)N(1 \le N \le 1000), M(1 \le M \le 5)N(1≤N≤1000),M(1≤M≤5) and K(−1000≤K≤1000K(-1000 \le K \le 1000K(−1000≤K≤1000), the second line contains NNN non-zero integers: a[1],a[2],…,aNa[1], a[2], …, a[N](-1000 \le a[i] \le 1000)a[1],a[2],…,aN, and the third line contains MMM characters: f[1],f[2],…,f[M](f[j]=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

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

题意:

有n个房间,m个诅咒,每个房间有一个数值,刚开始有一个初始值,每次进入一个房间可以选择消除诅咒或者不消除,消除诅咒只能顺序消除,消除诅咒就是拿初始值和房间的数值做运算,求最后最大的数是多少

分析:

Max[i][j] 表示 第i个房间 第j个操作的最大值, Min[i][j]

表示第i个房间第j个操作的最小值

因为乘法 负负相乘可能变得很大

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1010;
const ll INF = 0x3f3f3f3f3f3f3f3f;

int T,n,m;
int arr[N];
char f[10];
ll Max[N][10],Min[N][10];

ll k,ans;

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%lld",&n,&m,&k);
        for(int i = 1; i <= n; i++) scanf("%d",&arr[i]);
        scanf("%s",f+1);

        memset(Max,-0x3f,sizeof(Max));
        memset(Min,0x3f,sizeof(Min));
        Max[0][0] = Min[0][0] = k;
        ans = -INF;

        for(int i = 1; i <= n; i++){
            Max[i][0] = k;
            Min[i][0] = k;//没有运算符的时候就是初始值k
            for(int j = 1; j <= min(m,i); j++){
                Max[i][j] = Max[i-1][j];
                Min[i][j] = Min[i-1][j];//当前i房间第j个运算符先初始化为上一个房间第j个运算符的值
                ll a = Max[i-1][j-1],c = Min[i-1][j-1],b = (ll)arr[i];//当前i房间第j个运算符的值应该由上一个房间的j-1运算符转移过来

                if(f[j] == '+'){
                    a += b,c += b;
                }
                else if(f[j] == '-'){
                    a -= b,c -= b;
                }
                else if(f[j] == '*'){
                    a *= b,c *= b;
                }
                else if(f[j] == '/'){
                    a /= b,c /= b;
                }
                if(a < c) swap(a,c);
                Max[i][j] = max(Max[i][j],a);
                Min[i][j] = min(Min[i][j],c);
            }
            ans = max(Max[i][m],ans);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/82720507