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 Nge 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
题目来源
题意:
有1-n间房 每间有一个数ai
有1-m个操作fj 每种操作可能是+-*/
有一个初始值k 走到第i个房间如果进行了第j个操作 得到结果k fj ai
房间和操作的顺序不能改变
问最后得到的最大值
思路:
就是一个比较简单的dp 发现自己dp总是写不好
最近不如多练点dp吧
dp[i][j]表示在第i间房做j个操作 i一定是不能小于j
加和减的话比较常规 乘除涉及到负数的话就不一定了
所以需要既存最大值也要存最小值
还要注意初始化的赋值
1 //#include"pch.h" 2 3 #include<iostream> 4 #include<stdio.h> 5 #include<string.h> 6 #include<algorithm> 7 #include<stack> 8 #include<queue> 9 #include<map> 10 #include<vector> 11 #include<cmath> 12 #include<cstring> 13 #include<set> 14 #include<stack> 15 //#include<bits/stdc++.h> 16 17 #define inf 0x3f3f3f3f 18 using namespace std; 19 typedef long long LL; 20 21 const int maxn = 1005; 22 int t; 23 int n, m, k; 24 int a[maxn]; 25 LL dpmin[maxn][10], dpmax[maxn][10]; 26 char f[10]; 27 28 int main() 29 { 30 31 scanf("%d", &t); 32 while (t--) { 33 memset(dpmax, -inf, sizeof(dpmax)); 34 memset(dpmin, inf, sizeof(dpmin)); 35 //cout<<dpmax[0][0]<<endl<<dpmin[0][0]<<endl; 36 scanf("%d%d%d", &n, &m, &k); 37 for (int i = 1; i <= n; i++) { 38 scanf("%d", &a[i]); 39 } 40 getchar(); 41 for (int i = 1; i <= m; i++) { 42 scanf("%c", &f[i]); 43 } 44 45 for (int i = 0; i <= n; i++) { 46 dpmax[i][0] = dpmin[i][0] = k; 47 } 48 for (int j = 1; j <= m; j++) { 49 for (int i = j; i <= n; i++) { 50 dpmax[i][j] = dpmax[i - 1][j];//第i间不做 51 dpmin[i][j] = dpmin[i - 1][j]; 52 if (f[j] == '+') { 53 dpmax[i][j] = max(dpmax[i][j], dpmax[i - 1][j - 1] + a[i]); 54 dpmin[i][j] = min(dpmin[i][j], dpmin[i - 1][j - 1] + a[i]); 55 } 56 if (f[j] == '-') { 57 dpmax[i][j] = max(dpmax[i][j], dpmax[i - 1][j - 1] - a[i]); 58 dpmin[i][j] = min(dpmin[i][j], dpmin[i - 1][j - 1] - a[i]); 59 } 60 if (f[j] == '*') { 61 dpmax[i][j] = max(dpmax[i][j], dpmax[i - 1][j - 1] * a[i]); 62 dpmax[i][j] = max(dpmax[i][j], dpmin[i - 1][j - 1] * a[i]); 63 dpmin[i][j] = min(dpmin[i][j], dpmax[i - 1][j - 1] * a[i]); 64 dpmin[i][j] = min(dpmin[i][j], dpmin[i - 1][j - 1] * a[i]); 65 } 66 if (f[j] == '/') { 67 dpmax[i][j] = max(dpmax[i][j], dpmax[i - 1][j - 1] / a[i]); 68 dpmax[i][j] = max(dpmax[i][j], dpmin[i - 1][j - 1] / a[i]); 69 dpmin[i][j] = min(dpmin[i][j], dpmax[i - 1][j - 1] / a[i]); 70 dpmin[i][j] = min(dpmin[i][j], dpmin[i - 1][j - 1] / a[i]); 71 } 72 } 73 } 74 printf("%lld ", dpmax[n][m]); 75 } 76 return 0; 77 }