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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章