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 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 N\ge MNM). 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(1T1000), 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(1N1000),M(1M5) and K(-1000 \le K \le 1000K(1000K1000), 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](1000a[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

题目来源

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

题解:DP;

分别记录最大值和最小值,(因为可能出现两个都是负数的情况),转移方程为:

dp[i][j] = dp[i - 1][j];  dp1[i][j] = dp1[i - 1][j];分别记录最大和最小
对于不同符号,有不同的转移方程

参考代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <string>
 6 #include <algorithm>
 7 #include <bitset>
 8 #define INF 0x3f3f3f3f3f3f3f3fll
 9 #define clr(x, y) memset(x, y, sizeof(x))
10 #define mod 1000000007
11 using namespace std;
12 typedef long long LL;
13 const int maxn = 1010;
14 const int maxm = 10;
15 int a[maxn],t;
16 LL dp[maxn][maxm],dp1[maxn][maxn];
17 char f[maxm];
18 LL Max(LL a,LL b) {return a>=b? a:b; }
19 LL Min(LL a,LL b)  { return a<b? a:b; }
20 int main()
21 {
22     scanf("%d", &t);
23     while(t--)
24     {
25         memset(dp, -INF, sizeof dp);
26         memset(dp1,INF,sizeof dp1);
27         int n, m, k;
28         scanf("%d%d%d", &n, &m, &k);
29         for(int i = 1; i <= n; i++)
30         {
31             scanf("%d", &a[i]);
32             dp[i][0] =dp1[i][0]=k;
33         }
34         dp[0][0] = dp1[0][0] = k;
35         scanf("%s", f + 1);
36         for(int i = 1; i <= n; i++)
37         {
38             for(int j = 1; j <= m; j++)
39             {
40                 if(i<j) continue;
41                 dp[i][j] = dp[i - 1][j]; dp1[i][j] = dp1[i - 1][j];
42                 if(f[j] == '+') dp[i][j]=Max(dp[i][j],dp[i - 1][j - 1] + a[i]), dp1[i][j]=Min(dp1[i][j],dp1[i - 1][j - 1] + a[i]);
43                 else if(f[j] == '-') dp[i][j] = Max(dp[i][j], dp[i - 1][j - 1] - a[i]), dp1[i][j] = Min(dp1[i][j], dp1[i - 1][j - 1] - a[i]);
44                 else if(f[j] == '*')
45                 {
46                     dp[i][j] = Max(dp[i][j], Max(dp[i - 1][j - 1] * a[i],dp1[i - 1][j - 1] * a[i]) );
47                     dp1[i][j] = Min(dp1[i][j], Min(dp[i - 1][j - 1] * a[i],dp1[i - 1][j - 1] * a[i]) );
48                 } 
49                 else 
50                 {
51                     dp[i][j] = Max(dp[i][j], Max(dp[i - 1][j - 1] / a[i], dp1[i - 1][j - 1] / a[i]) );
52                     dp1[i][j]=Min(dp1[i][j], Min(dp[i - 1][j - 1] / a[i], dp1[i - 1][j - 1] / a[i]) );
53                 }
54             }
55         }
56         printf("%lld\n", Max(dp[n][m],dp1[n][m]) );
57     }
58     return 0;
59 }
View Code

 

  

posted @ 2018-09-15 19:13  StarHai  阅读(610)  评论(0编辑  收藏  举报