B: Mathematical Curse
时间限制: 1 Sec 内存限制: 128 MB题目描述
A prince of the Science Continent was im*ed 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 N rooms from the place where he was im*ed to the exit of the castle. In the i^th room, there is a wizard who has a resentment value of a[i]. The prince has M curses, the j^th curse is f[j], and f[j] represents one of the four arithmetic operations, namely addition('+'), subtraction('-'), multiplication('*'), and integer division (' / '). The prince’s initial resentment value is K. 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] with the wizard's resentment value. That is, if the prince eliminates the j^th curse in the i^th room, then his resentment value will change from x to (x f[j] a[i]), for example, when x=1, a[i]=2, f[j]='+', then x will become 1+2=3.
Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1] to a[N] in order and cannot turn back. He must also eliminate the f[1] to f[M] curses in order(It is guaranteed that N≥M). What is the maximum resentment value that the prince may have when he leaves the castle?
输入
The first line contains an integer 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) and K(-1000≤K≤1000), the second line contains N non-zero integers: a[1], a[2], … , a[N] (-1000≤a[i]≤1000), and the third line contains M characters: f[1], f[2], … , f[M] (f[j] 〖= 〗^' +^','-^','*',' / '), with no spaces in between.
输出
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 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define inf 0x3f3f3f 5 char opt[10]; 6 ll dp[10][1005],dpp[10][1005]; 7 int arr[1005]; 8 int main() 9 { 10 int t; 11 scanf("%d",&t); 12 while(t--) 13 { 14 int n,m; 15 ll k; 16 scanf("%d%d%lld",&n,&m,&k); 17 for(int i=1; i<=n; i++) 18 { 19 scanf("%d",&arr[i]); 20 } 21 scanf("%s",opt); 22 memset(dp,0,sizeof(dp)); 23 memset(dpp,0,sizeof(dpp)); 24 dp[0][0]=k; 25 dpp[0][0]=k; 26 ll ans=-inf; 27 for(int j=1; j<=n; j++) 28 { 29 dp[0][j]=k; 30 dpp[0][j]=k; 31 for(int i=0; i<m; i++) 32 { 33 dp[i+1][j]=inf; 34 dpp[i+1][j]=-inf; 35 if(i+1<=j-1) 36 { 37 dp[i+1][j]=min(dp[i+1][j],dp[i+1][j-1]); 38 dpp[i+1][j]=max(dpp[i+1][j],dpp[i+1][j-1]); 39 } 40 if(opt[i]=='+') 41 { 42 dp[i+1][j]=min(dp[i+1][j],dp[i][j-1]+arr[j]); 43 dpp[i+1][j]=max(dpp[i+1][j],dpp[i][j-1]+arr[j]); 44 } 45 else if(opt[i]=='-') 46 { 47 dp[i+1][j]=min(dp[i+1][j],dp[i][j-1]-arr[j]); 48 dpp[i+1][j]=max(dpp[i+1][j],dpp[i][j-1]-arr[j]); 49 } 50 else if(opt[i]=='*') 51 { 52 dp[i+1][j]=min(dp[i+1][j],dp[i][j-1]*arr[j]); 53 dp[i+1][j]=min(dp[i+1][j],dpp[i][j-1]*arr[j]); 54 dpp[i+1][j]=max(dpp[i+1][j],dpp[i][j-1]*arr[j]); 55 dpp[i+1][j]=max(dpp[i+1][j],dp[i][j-1]*arr[j]); 56 } 57 else if(opt[i]=='/'&&arr[j]!=0) 58 { 59 dp[i+1][j]=min(dp[i+1][j],dp[i][j-1]/arr[j]); 60 dp[i+1][j]=min(dp[i+1][j],dpp[i][j-1]/arr[j]); 61 dpp[i+1][j]=max(dpp[i+1][j],dpp[i][j-1]/arr[j]); 62 dpp[i+1][j]=max(dpp[i+1][j],dp[i][j-1]/arr[j]); 63 } 64 if(i==m-1&&dpp[i+1][j]>ans&&dpp[i+1][j]!=inf&&i+1<=j) 65 { 66 ans=dpp[i+1][j]; 67 } 68 } 69 } 70 printf("%lld\n",ans); 71 } 72 return 0; 73 }View Code